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

validate reactor messages #2711

Merged
merged 26 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e7ca52b
validate reactor messages
melekes Oct 26, 2018
69ea74e
validate blockchain messages
melekes Oct 26, 2018
4572b92
validate evidence messages
melekes Oct 26, 2018
db699f6
todo
melekes Oct 28, 2018
53a4660
check ProposalPOL and signature sizes
melekes Oct 29, 2018
fc5f868
add a changelog entry
melekes Oct 29, 2018
57163e9
check addr is valid when we add it to the addrbook
melekes Oct 29, 2018
8bf215f
validate incoming netAddr (not just nil check!)
melekes Oct 29, 2018
866af23
fixes after Bucky's review
melekes Oct 30, 2018
8f8aae5
check timestamps
melekes Oct 30, 2018
daa6bb9
beef up block#ValidateBasic
melekes Oct 30, 2018
e6aaed4
move some checks into bcBlockResponseMessage
melekes Oct 30, 2018
3cc3c8e
update Gopkg.lock
melekes Oct 31, 2018
22e7333
bump year since now we check it
melekes Oct 31, 2018
ceabe99
generate test/p2p/data on the fly using tendermint testnet
melekes Oct 31, 2018
e46b35b
allow sync chains older than 1 year
melekes Oct 31, 2018
015a33d
use full path when creating a testnet
melekes Oct 31, 2018
9baaa4b
move testnet gen to test/docker/Dockerfile
melekes Oct 31, 2018
24f4e1e
relax LastCommitRound check
melekes Oct 31, 2018
8aa18f8
Merge branch 'develop' into 2683-validate-reactor-messages
ebuchman Oct 31, 2018
c4817c7
fix conflicts after merge
melekes Oct 31, 2018
43f3d14
add small comment
melekes Oct 31, 2018
0a3d52e
some ValidateBasic updates
ebuchman Oct 31, 2018
1c5088e
fixes
ebuchman Nov 1, 2018
46388ae
Merge pull request #2738 from tendermint/bucky/validate-basic
ebuchman Nov 1, 2018
20149a4
AppHash length is not fixed
ebuchman Nov 1, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ Friendly reminder, we have a [bug bounty program](https://hackerone.com/tendermi
github.com/tendermint/crypto
- [tools] [\#2238](https://github.com/tendermint/tendermint/issues/2238) Binary dependencies are now locked to a specific git commit
- [libs/log] [\#2706](https://github.com/tendermint/tendermint/issues/2706) Add year to log format
- [consensus] [\#2683] validate all incoming messages
- [evidence] [\#2683] validate all incoming messages
- [blockchain] [\#2683] validate all incoming messages
- [p2p/pex] [\#2683] validate pexAddrsMessage addresses

### BUG FIXES:
- [autofile] [\#2428](https://github.com/tendermint/tendermint/issues/2428) Group.RotateFile need call Flush() before rename (@goolAdapter)
Expand Down
66 changes: 64 additions & 2 deletions blockchain/reactor.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package blockchain

import (
"errors"
"fmt"
"reflect"
"time"

amino "github.com/tendermint/go-amino"

"github.com/tendermint/tendermint/crypto"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/p2p"
Expand Down Expand Up @@ -180,6 +182,12 @@ func (bcR *BlockchainReactor) Receive(chID byte, src p2p.Peer, msgBytes []byte)
return
}

if err = msg.ValidateBasic(); err != nil {
bcR.Logger.Error("Peer sent us invalid msg", "peer", src, "msg", msg, "err", err)
bcR.Switch.StopPeerForError(src, err)
return
}

bcR.Logger.Debug("Receive", "src", src, "chID", chID, "msg", msg)

switch msg := msg.(type) {
Expand All @@ -188,7 +196,6 @@ func (bcR *BlockchainReactor) Receive(chID byte, src p2p.Peer, msgBytes []byte)
// Unfortunately not queued since the queue is full.
}
case *bcBlockResponseMessage:
// Got a block.
bcR.pool.AddBlock(src.ID(), msg.Block, len(msgBytes))
case *bcStatusRequestMessage:
// Send peer our state.
Expand Down Expand Up @@ -352,7 +359,9 @@ func (bcR *BlockchainReactor) BroadcastStatusRequest() error {
// Messages

// BlockchainMessage is a generic message for this reactor.
type BlockchainMessage interface{}
type BlockchainMessage interface {
ValidateBasic() error
melekes marked this conversation as resolved.
Show resolved Hide resolved
}

func RegisterBlockchainMessages(cdc *amino.Codec) {
cdc.RegisterInterface((*BlockchainMessage)(nil), nil)
Expand All @@ -377,6 +386,14 @@ type bcBlockRequestMessage struct {
Height int64
}

// ValidateBasic performs basic validation.
func (m *bcBlockRequestMessage) ValidateBasic() error {
if m.Height < 0 {
return errors.New("Negative Height")
ebuchman marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}

func (m *bcBlockRequestMessage) String() string {
return fmt.Sprintf("[bcBlockRequestMessage %v]", m.Height)
}
Expand All @@ -385,6 +402,14 @@ type bcNoBlockResponseMessage struct {
Height int64
}

// ValidateBasic performs basic validation.
func (m *bcNoBlockResponseMessage) ValidateBasic() error {
if m.Height < 0 {
return errors.New("Negative Height")
}
return nil
}

func (brm *bcNoBlockResponseMessage) String() string {
return fmt.Sprintf("[bcNoBlockResponseMessage %d]", brm.Height)
}
Expand All @@ -395,6 +420,27 @@ type bcBlockResponseMessage struct {
Block *types.Block
}

// ValidateBasic performs basic validation.
func (m *bcBlockResponseMessage) ValidateBasic() error {
if err := m.Block.ValidateBasic(); err != nil {
return err
}

// Note we are more strict about the fields below in state#ValidateBlock.
if m.Block.TotalTxs < m.Block.NumTxs {
return errors.New("Header.TotalTxs is less than Header.NumTxs")
}
if err := m.Block.LastBlockID.ValidateBasic(); err != nil {
return fmt.Errorf("Wrong Header.LastBlockID: %v", err)
}
if len(m.Block.ProposerAddress) != crypto.AddressSize {
return fmt.Errorf("Expected len(Header.ProposerAddress) to be %d",
crypto.AddressSize)
}

return nil
}

func (m *bcBlockResponseMessage) String() string {
return fmt.Sprintf("[bcBlockResponseMessage %v]", m.Block.Height)
}
Expand All @@ -405,6 +451,14 @@ type bcStatusRequestMessage struct {
Height int64
}

// ValidateBasic performs basic validation.
func (m *bcStatusRequestMessage) ValidateBasic() error {
if m.Height < 0 {
return errors.New("Negative Height")
}
return nil
}

func (m *bcStatusRequestMessage) String() string {
return fmt.Sprintf("[bcStatusRequestMessage %v]", m.Height)
}
Expand All @@ -415,6 +469,14 @@ type bcStatusResponseMessage struct {
Height int64
}

// ValidateBasic performs basic validation.
func (m *bcStatusResponseMessage) ValidateBasic() error {
if m.Height < 0 {
return errors.New("Negative Height")
}
return nil
}

func (m *bcStatusResponseMessage) String() string {
return fmt.Sprintf("[bcStatusResponseMessage %v]", m.Height)
}
2 changes: 1 addition & 1 deletion config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func ResetTestRoot(testName string) *Config {
}

var testGenesis = `{
"genesis_time": "2017-10-10T08:20:13.695936996Z",
"genesis_time": "2018-10-10T08:20:13.695936996Z",
"chain_id": "tendermint_test",
"validators": [
{
Expand Down
2 changes: 0 additions & 2 deletions consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,6 @@ func randGenesisDoc(numValidators int, randPower bool, minPower int64) (*types.G
func randGenesisState(numValidators int, randPower bool, minPower int64) (sm.State, []types.PrivValidator) {
genDoc, privValidators := randGenesisDoc(numValidators, randPower, minPower)
s0, _ := sm.MakeGenesisState(genDoc)
db := dbm.NewMemDB() // remove this ?
sm.SaveState(db, s0)
return s0, privValidators
}

Expand Down
1 change: 0 additions & 1 deletion consensus/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/tendermint/tendermint/abci/example/code"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/tendermint/tendermint/types"
)

Expand Down