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

Use invalid opcode to consume all gas in MinimalForwarder #3035

Merged
merged 3 commits into from
Dec 22, 2021

Conversation

Amxx
Copy link
Collaborator

@Amxx Amxx commented Dec 17, 2021

Fixes #2864

PR Checklist

  • Tests
  • Documentation
  • Changelog entry

@Amxx Amxx mentioned this pull request Dec 17, 2021
test/metatx/MinimalForwarder.test.js Outdated Show resolved Hide resolved
contracts/metatx/MinimalForwarder.sol Outdated Show resolved Hide resolved
Copy link
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

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

Thanks!

@frangio frangio changed the title Use invalid opcode to consume all gas Use invalid opcode to consume all gas in MinimalForwarder Dec 22, 2021
@frangio frangio merged commit 10c8fcd into OpenZeppelin:master Dec 22, 2021
@Amxx Amxx deleted the feature/invalid-opcode branch December 23, 2021 09:36
@lastperson
Copy link
Contributor

@Amxx hey, I'm trying to understand the issue solved here. Could you explain why burning all gas is important and what it prevents from happening?

@frangio
Copy link
Contributor

frangio commented Jul 27, 2022

@lastperson
Copy link
Contributor

@frangio the mentioned fix is only helpful for Forwarder calling another Forwarder. Though it still doesn't guarantee that the target contract gets specified amount of gas.

@frangio
Copy link
Contributor

frangio commented Jul 28, 2022

Can you elaborate?

@lastperson
Copy link
Contributor

Sure. Consider the target contract

contract Target {
    function win(address who) external {
        require(gasleft() > 900000);
        IERC20(0x1234...1234).transfer(who, 100e18);
    }
}

User creates a meta tx request with req.gas = 1000000. Relayer picks it up and sends it with 500000 gas.
Target.win() reverts but gasleft() > req.gas / 63 passes.

@Amxx
Copy link
Collaborator Author

Amxx commented Jul 29, 2022

Its a bit subtle, but here is what happens:

  • The forwarder is designed to check at the call in it, in case it run out of gas, was given the amount specified.
  • You target contract, does not revert because it ran out of gas (and was unable to execute because of that), it reverts because a require failed.

You contract check if there is a given amount of gas, but if that is not the case, it doesn't not trigger an out of gas exception.
I'm pretty sure your Target contract is an example for the sake of discussion, but IMO it showcases possible bad design by def.

IMO, contract should not check gas that way.

  • They should try to perform operation, and fail with out of gas if that was not enough.
  • If they want to guarantee that a subcall has enough gas, they should replicate the Forwarder design (send gas, and if faillure and not enough left, do an invalid call)
  • If you really want to ensure gasleft() > 900000, don't do it in a require, do if (gasleft() < 900000) assembly { invalid() }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use invalid opcode in MinimalForwarder
3 participants