forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 5
feat: reentrancy.Guard
for stateful precompiles
#212
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
edc0648
feat: `PrecompileEnvironment.ReentrancyGuard()`
ARR4N 9148751
doc: `ReentrancyGuard()` and warning on `Call()`
ARR4N 2176379
Merge branch 'main' into arr4n/reentrancy-guard
ARR4N 7b43105
Merge branch 'main' into arr4n/reentrancy-guard
ARR4N b979e96
refactor: move to `reentrancy` package
ARR4N e6aea80
chore: placate the linter
ARR4N e8dbf7a
chore: actually, that was `go mod tidy`
ARR4N ff0ade2
fix: import correct `assert` package
ARR4N 28b1c28
chore: `go mod tidy`
ARR4N 358a2d2
test: different combinations of self address and key
ARR4N 8bdefb9
Merge branch 'main' into arr4n/reentrancy-guard
ARR4N 2f74e67
chore: nice work, linter
ARR4N File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2025 the libevm authors. | ||
// | ||
// The libevm additions to go-ethereum are free software: you can redistribute | ||
// them and/or modify them under the terms of the GNU Lesser General Public License | ||
// as published by the Free Software Foundation, either version 3 of the License, | ||
// or (at your option) any later version. | ||
// | ||
// The libevm additions are distributed in the hope that they will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser | ||
// General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see | ||
// <http://www.gnu.org/licenses/>. | ||
|
||
// Package reentrancy provides a reentrancy guard for stateful precompiles that | ||
// make outgoing calls to other contracts. | ||
// | ||
// Reentrancy occurs when the contract (C) called by a precompile (P) makes a | ||
// further call back into P, which may result in theft of funds (see DAO hack). | ||
// A reentrancy guard detects these recursive calls and reverts. | ||
package reentrancy | ||
|
||
import ( | ||
"github.com/ava-labs/libevm/common" | ||
"github.com/ava-labs/libevm/core/vm" | ||
"github.com/ava-labs/libevm/crypto" | ||
"github.com/ava-labs/libevm/libevm" | ||
) | ||
|
||
var slotPreimagePrefix = []byte("libevm-reentrancy-guard-") | ||
|
||
// Guard returns [vm.ErrExecutionReverted] i.f.f. it has already been called | ||
// with the same `key`, by the same contract, in the same transaction. It | ||
// otherwise returns nil. The `key` MAY be nil. | ||
// | ||
// Contract equality is defined as the [libevm.AddressContext] "self" address | ||
// being the same under EVM semantics. | ||
func Guard(env vm.PrecompileEnvironment, key []byte) error { | ||
self := env.Addresses().EVMSemantic.Self | ||
slot := crypto.Keccak256Hash(slotPreimagePrefix, key) | ||
|
||
sdb := env.StateDB() | ||
if sdb.GetTransientState(self, slot) != (common.Hash{}) { | ||
return vm.ErrExecutionReverted | ||
} | ||
sdb.SetTransientState(self, slot, common.Hash{1}) | ||
return nil | ||
} | ||
|
||
// Keep the `libevm` import to allow the linked comment on [Guard]. The package | ||
// is imported by `vm` anyway so this is a noop but it improves developer | ||
// experience. | ||
var _ = (*libevm.AddressContext)(nil) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2025 the libevm authors. | ||
// | ||
// The libevm additions to go-ethereum are free software: you can redistribute | ||
// them and/or modify them under the terms of the GNU Lesser General Public License | ||
// as published by the Free Software Foundation, either version 3 of the License, | ||
// or (at your option) any later version. | ||
// | ||
// The libevm additions are distributed in the hope that they will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser | ||
// General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see | ||
// <http://www.gnu.org/licenses/>. | ||
|
||
package reentrancy | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/holiman/uint256" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/libevm/common" | ||
"github.com/ava-labs/libevm/core/vm" | ||
"github.com/ava-labs/libevm/libevm" | ||
"github.com/ava-labs/libevm/libevm/ethtest" | ||
"github.com/ava-labs/libevm/libevm/hookstest" | ||
) | ||
|
||
func TestGuard(t *testing.T) { | ||
sut := common.HexToAddress("7E57ED") | ||
eve := common.HexToAddress("BAD") | ||
eveCalled := false | ||
|
||
zero := func() *uint256.Int { | ||
return uint256.NewInt(0) | ||
} | ||
|
||
returnIfGuarded := []byte("guarded") | ||
|
||
hooks := &hookstest.Stub{ | ||
PrecompileOverrides: map[common.Address]libevm.PrecompiledContract{ | ||
eve: vm.NewStatefulPrecompile(func(env vm.PrecompileEnvironment, input []byte) (ret []byte, err error) { | ||
eveCalled = true | ||
return env.Call(sut, []byte{}, env.Gas(), zero()) // i.e. reenter | ||
}), | ||
sut: vm.NewStatefulPrecompile(func(env vm.PrecompileEnvironment, input []byte) (ret []byte, err error) { | ||
// The argument is optional and used only to allow more than one | ||
// guard in a contract. | ||
if err := Guard(env, nil); err != nil { | ||
return returnIfGuarded, err | ||
} | ||
if env.Addresses().EVMSemantic.Caller == eve { | ||
// A real precompile MUST NOT panic under any circumstances. | ||
// It is done here to avoid a loop should the guard not | ||
// work. | ||
panic("reentrancy") | ||
} | ||
return env.Call(eve, []byte{}, env.Gas(), zero()) | ||
}), | ||
}, | ||
} | ||
hooks.Register(t) | ||
|
||
_, evm := ethtest.NewZeroEVM(t) | ||
got, _, err := evm.Call(vm.AccountRef{}, sut, []byte{}, 1e6, zero()) | ||
require.True(t, eveCalled, "Malicious contract called") | ||
// The error is propagated Guard() -> reentered SUT -> Eve -> top-level SUT -> evm.Call() | ||
// This MUST NOT be [assert.ErrorIs] as such errors are never wrapped in geth. | ||
assert.Equal(t, err, vm.ErrExecutionReverted, "Precompile reverted") | ||
assert.Equal(t, returnIfGuarded, got, "Precompile reverted with expected data") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.