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

Improper Validation Of create2 Return Value #1323

Closed
code423n4 opened this issue Aug 4, 2023 · 6 comments
Closed

Improper Validation Of create2 Return Value #1323

code423n4 opened this issue Aug 4, 2023 · 6 comments
Labels
bug Something isn't working disagree with severity Sponsor confirms validity, but disagrees with warden’s risk assessment (sponsor explain in comments) downgraded by judge Judge downgraded the risk level of this issue grade-b primary issue Highest quality submission among a set of duplicates QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Lines of code

https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/Penrose.sol#L362-L376
https://github.com/Tapioca-DAO/tapioca-periph-audit/blob/023751a4e987cf7c203ab25d3abba58f7344f213/contracts/TapiocaDeployer/TapiocaDeployer.sol#L22-L50
https://github.com/boringcrypto/BoringSolidity/blob/master/contracts/BoringFactory.sol#L32-L68

Vulnerability details

Impact

There is no validation for deployed contracts using BoringFactory whether the returned address is non-zero address and check for the size of code. This will result in setting zero address and nonexisting address as isMarketRegistered equal to true.

Proof of Concept

Inside Penrose.sol#registerSingularity() it uses deploy() function of BoringFactory.deploy() function of BoringFactory does not revert properly if there is a failed contract deployment or revert from the create2 opcode as it does not properly check the returned address for bytecode and for non-zero address. The create2 opcode returns the expected address which will never be the zero address. I have created an issue on BoringSolidity library about this but never received any response till now.

The same issue was found by leastwood on the mochi protocol. In their case BeaconProxyDeployer library was vulnerable and in our case BoringSolidity's BoringFactory library is vulnerable.
issue by leastwood : code-423n4/2021-10-mochi-findings#155

I saw another place in the contract where while deploying the contract using create2 it checks for zero address but didn't check for size of code

here : https://github.com/Tapioca-DAO/tapioca-periph-audit/blob/023751a4e987cf7c203ab25d3abba58f7344f213/contracts/TapiocaDeployer/TapiocaDeployer.sol#L22-L50

Tools Used

Manual code review
Discussions with the LZ team

Recommended Mitigation Steps

Check whether the returned address is non-zero address and size of address

Fix for Penrose.sol#registerSingularity()

    function registerSingularity(
        address mc,
        bytes calldata data,
        bool useCreate2
    )
        external
        payable
        onlyOwner
        registeredSingularityMasterContract(mc)
        returns (address _contract)
    {
        _contract = deploy(mc, data, useCreate2);
        require(extcodesize(_contract) != 0, "returned contract don't have size");
        require(_contract != address(0), "zero address");
        isMarketRegistered[_contract] = true;
        emit RegisterSingularity(_contract, mc);
    }

Fix for TapiocaDeployer.sol#deploy()

function deploy(
        uint256 amount,
        bytes32 salt,
        bytes memory bytecode,
        string memory contractName
    ) external payable returns (address addr) {
        require(
            address(this).balance >= amount,
            "Create2: insufficient balance"
        );
        require(
            bytecode.length != 0,
            string.concat(
                "Create2: bytecode length is zero for contract ",
                contractName
            )
        );
        /// @solidity memory-safe-assembly
        assembly {
            addr := create2(amount, add(bytecode, 0x20), mload(bytecode), salt)
        }
        require(extcodesize(addr) != 0, "contract didn't have size");
        require(
            addr != address(0),
            string.concat(
                "Create2: Failed on deploy for contract ",
                contractName
            )
        );
    }

Assessed type

Library

@code423n4 code423n4 added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working labels Aug 4, 2023
code423n4 added a commit that referenced this issue Aug 4, 2023
@c4-pre-sort c4-pre-sort added the primary issue Highest quality submission among a set of duplicates label Aug 9, 2023
@c4-pre-sort
Copy link

minhquanym marked the issue as primary issue

@c4-sponsor
Copy link

0xRektora marked the issue as disagree with severity

@c4-sponsor c4-sponsor added the disagree with severity Sponsor confirms validity, but disagrees with warden’s risk assessment (sponsor explain in comments) label Aug 25, 2023
@0xRektora
Copy link

Informational. Penrose can unregister a SGL if it is invalid. TapiocaDeployer is used only on Protocol genesis. No harm done.

@c4-sponsor
Copy link

0xRektora marked the issue as sponsor confirmed

@c4-sponsor c4-sponsor added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Aug 25, 2023
@c4-judge c4-judge added downgraded by judge Judge downgraded the risk level of this issue QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax and removed 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value labels Sep 27, 2023
@c4-judge
Copy link

dmvt changed the severity to QA (Quality Assurance)

@c4-judge
Copy link

dmvt marked the issue as grade-b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working disagree with severity Sponsor confirms validity, but disagrees with warden’s risk assessment (sponsor explain in comments) downgraded by judge Judge downgraded the risk level of this issue grade-b primary issue Highest quality submission among a set of duplicates QA (Quality Assurance) Assets are not at risk. State handling, function incorrect as to spec, issues with clarity, syntax sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

6 participants