Giter Site home page Giter Site logo

excessivelysafecall's Introduction

ExcessivelySafeCall

This solidity library helps you call untrusted contracts safely. Specifically, it seeks to prevent all possible ways that the callee can maliciously cause the caller to revert. Most of these revert cases are covered by the use of a low-level call. The main difference with between address.call()call and address.excessivelySafeCall() is that a regular solidity call will automatically copy bytes to memory without consideration of gas.

This is to say, a low-level solidity call will copy any amount of bytes to local memory. When bytes are copied from returndata to memory, the memory expansion cost is paid. This means that when using a standard solidity call, the callee can "returnbomb" the caller, imposing an arbitrary gas cost. Because this gas is paid by the caller and in the caller's context, it can cause the caller to run out of gas and halt execution.

To prevent returnbombing, we provide excessivelySafeCall and excessivelySafeStaticCall. These behave similarly to solidity's low-level calls, however, they allow the user to specify a maximum number of bytes to be copied to local memory. E.g. a user desiring a single return value should specify a _maxCopy of 32 bytes. Refusing to copy large blobs to local memory effectively prevents the callee from triggering local OOG reversion. We also recommend careful consideration of the gas amount passed to untrusted callees.

Consider the following contracts:

contract BadGuy {
    function youveActivateMyTrapCard() external pure returns (bytes memory) {
        assembly{
            revert(0, 1_000_000)
        }
    }
}

contract Mark {
    function oops(address badGuy) {
        bool success;
        bytes memory ret;

        // Mark pays a lot of gas for this copy ๐Ÿ˜ฌ๐Ÿ˜ฌ๐Ÿ˜ฌ
        (success, ret) == badGuy.call(
            SOME_GAS,
            abi.encodeWithSelector(
                BadGuy.youveActivateMyTrapCard.selector
            )
        );

        // Mark may OOG here, preventing local state changes
        importantCleanup();
    }
}

contract ExcessivelySafeSam {
    using ExcessivelySafeCall for address;

    // Sam is cool and doesn't get returnbombed
    function sunglassesEmoji(address badGuy) {
        bool success;
        bytes memory ret;

        (success, ret) == badGuy.excessivelySafeCall(
            SOME_GAS,
            32,  // <-- the magic. Copy no more than 32 bytes to memory
            abi.encodeWithSelector(
                BadGuy.youveActivateMyTrapCard.selector
            )
        );

        // Sam can afford to clean up after himself.
        importantCleanup();
    }
}

When would I use this

ExcessivelySafeCall prevents malicious callees from affecting post-execution cleanup (e.g. state-based replay protection). Given that a dev is unlikely to hard-code a call to a malicious contract, we expect most danger to come from dynamic dispatch protocols, where neither the callee nor the code being called is known to the developer ahead of time.

Dynamic dispatch in solidity is probably most useful for metatransaction protocols. This includes gas-abstraction relayers, smart contract wallets, bridges, etc.

Nomad uses excessively safe calls for safe processing of cross-domain messages. This guarantees that a message recipient cannot interfere with safe operation of the cross-domain communication channel and message processing layer.

Interacting with the repo

To install in your project:

  • install Foundry
  • forge install nomad-xyz/ExcessivelySafeCall

To run tests:

A note on licensing:

Tests are licensed GPLv3, as they extend the DSTest contract. Non-test work is avialable under user's choice of MIT and Apache2.0.

excessivelysafecall's People

Contributors

imti avatar prestwich avatar smartcontracts avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

daroko0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.