From 5f9239595b4913ccb0ed5ed77b33cf8499494613 Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Thu, 12 Nov 2020 12:28:19 -0800 Subject: [PATCH] Mitigate potential overflow. ethereum/eth2.0-specs#2129 (#7795) --- beacon-chain/core/helpers/block.go | 5 +++++ beacon-chain/core/helpers/block_test.go | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/beacon-chain/core/helpers/block.go b/beacon-chain/core/helpers/block.go index 86d22886c..d964170ef 100644 --- a/beacon-chain/core/helpers/block.go +++ b/beacon-chain/core/helpers/block.go @@ -1,6 +1,8 @@ package helpers import ( + "math" + "github.com/pkg/errors" stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" "github.com/prysmaticlabs/prysm/shared/params" @@ -17,6 +19,9 @@ import ( // assert slot < state.slot <= slot + SLOTS_PER_HISTORICAL_ROOT // return state.block_roots[slot % SLOTS_PER_HISTORICAL_ROOT] func BlockRootAtSlot(state *stateTrie.BeaconState, slot uint64) ([]byte, error) { + if math.MaxUint64-slot < params.BeaconConfig().SlotsPerHistoricalRoot { + return []byte{}, errors.New("slot overflows uint64") + } if slot >= state.Slot() || state.Slot() > slot+params.BeaconConfig().SlotsPerHistoricalRoot { return []byte{}, errors.Errorf("slot %d out of bounds", slot) } diff --git a/beacon-chain/core/helpers/block_test.go b/beacon-chain/core/helpers/block_test.go index bd58eecc2..44f8b41b0 100644 --- a/beacon-chain/core/helpers/block_test.go +++ b/beacon-chain/core/helpers/block_test.go @@ -2,6 +2,7 @@ package helpers_test import ( "fmt" + "math" "testing" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" @@ -101,6 +102,11 @@ func TestBlockRootAtSlot_OutOfBounds(t *testing.T) { stateSlot: params.BeaconConfig().SlotsPerHistoricalRoot + 2, expectedErr: "slot 1 out of bounds", }, + { + slot: math.MaxUint64 - 5, + stateSlot: 0, // Doesn't matter + expectedErr: "slot overflows uint64", + }, } for _, tt := range tests { state.Slot = tt.stateSlot