Merge pull request #2591 from rlan35/audit_fixes

fix peckshield audit issues on slashing
pull/2605/head
Rongjian Lan 5 years ago committed by GitHub
commit 0fa21786f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      consensus/votepower/roster.go
  2. 16
      core/blockchain.go
  3. 3
      staking/apr/compute.go
  4. 7
      staking/slash/double-sign.go

@ -3,10 +3,11 @@ package votepower
import ( import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"github.com/harmony-one/harmony/internal/utils"
"math/big" "math/big"
"sort" "sort"
"github.com/harmony-one/harmony/internal/utils"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/bls/ffi/go/bls"
common2 "github.com/harmony-one/harmony/internal/common" common2 "github.com/harmony-one/harmony/internal/common"

@ -2039,12 +2039,24 @@ func (bc *BlockChain) AddPendingSlashingCandidates(
pendingSlashes := append( pendingSlashes := append(
bc.pendingSlashes, current.SetDifference(candidates)..., bc.pendingSlashes, current.SetDifference(candidates)...,
) )
if l, c := len(pendingSlashes), len(current); l > maxPendingSlashes { state, err := bc.State()
if err != nil {
return err
}
valid := slash.Records{}
for i := range pendingSlashes {
if err := slash.Verify(bc, state, &pendingSlashes[i]); err == nil {
valid = append(valid, pendingSlashes[i])
}
}
if l, c := len(valid), len(current); l > maxPendingSlashes {
return errors.Wrapf( return errors.Wrapf(
errExceedMaxPendingSlashes, "current %d with-additional %d", c, l, errExceedMaxPendingSlashes, "current %d with-additional %d", c, l,
) )
} }
bc.pendingSlashes = pendingSlashes bc.pendingSlashes = valid
return bc.writeSlashes(bc.pendingSlashes) return bc.writeSlashes(bc.pendingSlashes)
} }

@ -1,9 +1,10 @@
package apr package apr
import ( import (
"math/big"
"github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/shard"
"math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/harmony/block" "github.com/harmony-one/harmony/block"

@ -119,6 +119,7 @@ var (
errAlreadyBannedValidator = errors.New("cannot slash on already banned validator") errAlreadyBannedValidator = errors.New("cannot slash on already banned validator")
errSignerKeyNotRightSize = errors.New("bls keys from slash candidate not right side") errSignerKeyNotRightSize = errors.New("bls keys from slash candidate not right side")
errSlashFromFutureEpoch = errors.New("cannot have slash from future epoch") errSlashFromFutureEpoch = errors.New("cannot have slash from future epoch")
errSlashBlockNoConflict = errors.New("cannot slash for signing on non-conflicting blocks")
) )
// MarshalJSON .. // MarshalJSON ..
@ -179,6 +180,10 @@ func Verify(
) )
} }
if first.ViewID != second.ViewID || first.Height != second.Height || first.BlockHeaderHash == second.BlockHeaderHash {
return errors.Wrapf(errSlashBlockNoConflict, "first %v+ second %v+", first, second)
}
if shard.CompareBlsPublicKey(first.SignerPubKey, second.SignerPubKey) != 0 { if shard.CompareBlsPublicKey(first.SignerPubKey, second.SignerPubKey) != 0 {
k1, k2 := first.SignerPubKey.Hex(), second.SignerPubKey.Hex() k1, k2 := first.SignerPubKey.Hex(), second.SignerPubKey.Hex()
return errors.Wrapf( return errors.Wrapf(
@ -226,7 +231,7 @@ func Verify(
if err := signature.Deserialize(ballot.Signature); err != nil { if err := signature.Deserialize(ballot.Signature); err != nil {
return err return err
} }
if err := first.SignerPubKey.ToLibBLSPublicKey(publicKey); err != nil { if err := ballot.SignerPubKey.ToLibBLSPublicKey(publicKey); err != nil {
return err return err
} }

Loading…
Cancel
Save