Skip to content

Commit

Permalink
Fix V-DOGE-VUL-005 & V-DOGE-VUL-006 (#39)
Browse files Browse the repository at this point in the history
* Fix crash when faulty node send nil header to syncer

* Fix crash when faulty node send single nil header to syncer

* Extract error constant

* Handle protocal Response nil Objs element

* Rename ErrNilHeaderRequest to ErrNilHeaderResponse
  • Loading branch information
DarianShawn authored May 19, 2022
1 parent 89a6991 commit 3c8a86f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 10 additions & 0 deletions protocol/skeleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package protocol

import (
"context"
"errors"
"fmt"

"github.com/dogechain-lab/jury/protocol/proto"
"github.com/dogechain-lab/jury/types"
)

var (
ErrNilHeaderResponse = errors.New("header response is nil")
)

func getHeaders(clt proto.V1Client, req *proto.GetHeadersRequest) ([]*types.Header, error) {
resp, err := clt.GetHeaders(context.Background(), req)
if err != nil {
Expand All @@ -17,6 +22,11 @@ func getHeaders(clt proto.V1Client, req *proto.GetHeadersRequest) ([]*types.Head
headers := []*types.Header{}

for _, obj := range resp.Objs {
if obj == nil || obj.Spec == nil {
// this nil header comes from a faulty node, reject all blocks of it.
return nil, ErrNilHeaderResponse
}

header := &types.Header{}
if err := header.UnmarshalRLP(obj.Spec.Value); err != nil {
return nil, err
Expand Down
17 changes: 13 additions & 4 deletions protocol/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ var (
ErrForkNotFound = errors.New("fork not found")
ErrPopTimeout = errors.New("timeout")
ErrConnectionClosed = errors.New("connection closed")
ErrTooManyHeaders = errors.New("unexpected more than 1 result")
ErrDecodeDifficulty = errors.New("failed to decode difficulty")
ErrInvalidTypeAssertion = errors.New("invalid type assertion")
)

// SyncPeer is a representation of the peer the node is syncing with
Expand Down Expand Up @@ -187,7 +190,7 @@ func statusFromProto(p *proto.V1Status) (*Status, error) {

diff, ok := new(big.Int).SetString(p.Difficulty, 10)
if !ok {
return nil, fmt.Errorf("failed to decode difficulty")
return nil, ErrDecodeDifficulty
}

s.Difficulty = diff
Expand Down Expand Up @@ -495,7 +498,7 @@ func (s *Syncer) DeletePeer(peerID peer.ID) error {
if ok {
syncPeer, ok := p.(*SyncPeer)
if !ok {
return errors.New("invalid type assertion")
return ErrInvalidTypeAssertion
}

if err := syncPeer.conn.Close(); err != nil {
Expand Down Expand Up @@ -720,12 +723,18 @@ func getHeader(clt proto.V1Client, num *uint64, hash *types.Hash) (*types.Header
}

if len(resp.Objs) != 1 {
return nil, fmt.Errorf("unexpected more than 1 result")
return nil, ErrTooManyHeaders
}

obj := resp.Objs[0]

if obj == nil || obj.Spec == nil || len(obj.Spec.Value) == 0 {
return nil, ErrNilHeaderResponse
}

header := &types.Header{}

if err := header.UnmarshalRLP(resp.Objs[0].Spec.Value); err != nil {
if err := header.UnmarshalRLP(obj.Spec.Value); err != nil {
return nil, err
}

Expand Down

0 comments on commit 3c8a86f

Please sign in to comment.