@@ -14,6 +14,7 @@ package iss
1414import (
1515 "encoding/binary"
1616 "fmt"
17+ "math"
1718
1819 es "github.com/go-errors/errors"
1920 "google.golang.org/protobuf/proto"
@@ -342,7 +343,7 @@ func New(
342343
343344 // Choose a leader for the new orderer instance.
344345 // TODO: Use the corresponding epoch's leader set to pick a leader, instead of just selecting one from all nodes.
345- leader := maputil .GetSortedKeys (membership .Nodes )[int (epoch )% len (membership .Nodes )]
346+ leader := maputil .GetSortedKeys (membership .Nodes )[int (epoch )% len (membership .Nodes )] //nolint:gosec
346347
347348 // Serialize checkpoint, so it can be proposed as a value.
348349 stableCheckpoint := checkpointpbtypes.StableCheckpoint {
@@ -408,7 +409,7 @@ func New(
408409 // that are not yet part of the system for those checkpoints.
409410 var delayed []stdtypes.NodeID
410411 for n := range membership .Nodes {
411- if epoch > iss .nodeEpochMap [n ]+ tt .EpochNr (iss .Params .RetainedEpochs ) {
412+ if epoch > iss .nodeEpochMap [n ]+ tt .EpochNr (iss .Params .RetainedEpochs ) { //nolint:gosec
412413 delayed = append (delayed , n )
413414 }
414415 }
@@ -438,7 +439,11 @@ func New(
438439
439440 sc := checkpoint .StableCheckpointFromPb (chkp .Pb ())
440441 // Check how far the received stable checkpoint is ahead of the local node's state.
441- chkpMembershipOffset := int (sc .Epoch ()) - 1 - int (iss .epoch .Nr ())
442+ if sc .Epoch () > math .MaxInt || iss .epoch .Nr () > math .MaxInt {
443+ return es .Errorf ("epoch number out of integer range" )
444+ }
445+ // Integer casting required here to prevent underflow.
446+ chkpMembershipOffset := int (sc .Epoch ()) - 1 - int (iss .epoch .Nr ()) //nolint:gosec
442447 if chkpMembershipOffset <= 0 {
443448 // Ignore stable checkpoints that are not far enough
444449 // ahead of the current state of the local node.
@@ -465,7 +470,11 @@ func New(
465470 }
466471
467472 chkp := checkpoint .StableCheckpointFromPb (c .checkpoint .Pb ())
468- chkpMembershipOffset := int (chkp .Epoch ()) - 1 - int (iss .epoch .Nr ())
473+ if chkp .Epoch () > math .MaxInt || iss .epoch .Nr () > math .MaxInt {
474+ return es .Errorf ("epoch number out of integer range" )
475+ }
476+ // Integer casting required here to prevent underflow.
477+ chkpMembershipOffset := int (chkp .Epoch ()) - 1 - int (iss .epoch .Nr ()) //nolint:gosec
469478 if chkpMembershipOffset <= 0 {
470479 // Ignore stable checkpoints that have been lagged behind
471480 // during validation
@@ -564,7 +573,7 @@ func InitialStateSnapshot(
564573 return nil , err
565574 }
566575
567- firstEpochLength := uint64 (params .SegmentLength * len (params .InitialMembership .Nodes ))
576+ firstEpochLength := uint64 (params .SegmentLength * len (params .InitialMembership .Nodes )) //nolint:gosec
568577 return & trantorpbtypes.StateSnapshot {
569578 AppData : appState ,
570579 EpochData : & trantorpbtypes.EpochData {
@@ -624,7 +633,7 @@ func (iss *ISS) initAvailability() {
624633 (* multisigcollector .InstanceParams )(& mscpbtypes.InstanceParams {
625634 Epoch : iss .epoch .Nr (),
626635 Membership : iss .memberships [0 ],
627- MaxRequests : uint64 (iss .Params .SegmentLength ),
636+ MaxRequests : uint64 (iss .Params .SegmentLength ), //nolint:gosec
628637 }),
629638 stdtypes .RetentionIndex (iss .epoch .Nr ()),
630639 )
@@ -640,12 +649,12 @@ func (iss *ISS) initOrderers() error {
640649
641650 // Create segment.
642651 // The sequence proposals are all set to nil, so that the orderer proposes new availability certificates.
643- proposals := freeProposals (iss .nextDeliveredSN + tt .SeqNr (i ), tt .SeqNr (len (leaders )), iss .Params .SegmentLength )
652+ proposals := freeProposals (iss .nextDeliveredSN + tt .SeqNr (i ), tt .SeqNr (len (leaders )), iss .Params .SegmentLength ) //nolint:gosec
644653 seg , err := common .NewSegment (leader , iss .epoch .Membership , proposals )
645654 if err != nil {
646655 return es .Errorf ("error creating new segment: %w" , err )
647656 }
648- iss .newEpochSN += tt .SeqNr (seg .Len ())
657+ iss .newEpochSN += tt .SeqNr (seg .Len ()) //nolint:gosec
649658
650659 // Instantiate a new PBFT orderer.
651660 stddsl .NewSubmodule (iss .m , iss .moduleConfig .Ordering ,
@@ -792,7 +801,7 @@ func (iss *ISS) advanceEpoch() error {
792801 EpochConfig : & trantorpbtypes.EpochConfig { // nolint:govet
793802 iss .epoch .Nr (),
794803 iss .epoch .FirstSN (),
795- uint64 (iss .epoch .Len ()),
804+ uint64 (iss .epoch .Len ()), //nolint:gosec
796805 iss .memberships ,
797806 },
798807 },
@@ -904,8 +913,9 @@ func (iss *ISS) deliverCommonCheckpoint(chkpData []byte) error {
904913 // The state to prune is determined according to the retention index
905914 // which is derived from the epoch number the new
906915 // stable checkpoint is associated with.
907- pruneIndex := int (chkp .Epoch ()) - iss .Params .RetainedEpochs
908- if pruneIndex > 0 { // "> 0" and not ">= 0", since only entries strictly smaller than the index are pruned.
916+ // Integer casting required here to prevent underflow.
917+ pruneIndex := int (chkp .Epoch ()) - iss .Params .RetainedEpochs //nolint:gosec
918+ if pruneIndex > 0 { // "> 0" and not ">= 0", since only entries strictly smaller than the index are pruned.
909919
910920 // Prune timer, checkpointing, availability, orderers, and other modules.
911921 stddsl .GarbageCollect (iss .m , iss .moduleConfig .Timer , stdtypes .RetentionIndex (pruneIndex ))
@@ -917,7 +927,7 @@ func (iss *ISS) deliverCommonCheckpoint(chkpData []byte) error {
917927
918928 // Prune epoch state.
919929 for epoch := range iss .epochs {
920- if epoch < tt .EpochNr (pruneIndex ) {
930+ if epoch < tt .EpochNr (pruneIndex ) { //nolint:gosec
921931 delete (iss .epochs , epoch )
922932 }
923933 }
@@ -931,7 +941,7 @@ func (iss *ISS) deliverCommonCheckpoint(chkpData []byte) error {
931941 // Note that we are not using the current epoch number here, because it is not relevant for checkpoints.
932942 // Using pruneIndex makes sure that the re-transmission is stopped
933943 // on every stable checkpoint (when another one is started).
934- stdtypes .RetentionIndex (pruneIndex ),
944+ stdtypes .RetentionIndex (pruneIndex ), //nolint:gosec
935945 isspbevents .PushCheckpoint (iss .moduleConfig .Self ).Pb (),
936946 )
937947
0 commit comments