@@ -19,6 +19,8 @@ type Logger interface {
19
19
}
20
20
21
21
const (
22
+ messageMaxSize = 2048
23
+
22
24
PROTOCOL_VERSION_0 = 0
23
25
PROTOCOL_VERSION_1 = 1
24
26
@@ -534,7 +536,10 @@ func Decode(rdr io.Reader) (PDU, error) {
534
536
}
535
537
536
538
if length < 8 {
537
- return nil , errors .New (fmt .Sprintf ("Wrong length: %d < 8" , length ))
539
+ return nil , fmt .Errorf ("Wrong length: %d < 8" , length )
540
+ }
541
+ if length > messageMaxSize {
542
+ return nil , fmt .Errorf ("Wrong length: %d > %d" , length , messageMaxSize )
538
543
}
539
544
toread := make ([]byte , length - 8 )
540
545
err = binary .Read (rdr , binary .BigEndian , toread )
@@ -545,7 +550,7 @@ func Decode(rdr io.Reader) (PDU, error) {
545
550
switch pduType {
546
551
case PDU_ID_SERIAL_NOTIFY :
547
552
if len (toread ) != 4 {
548
- return nil , errors . New ( fmt .Sprintf ("Wrong length for Serial Notify PDU: %d != 4" , len (toread ) ))
553
+ return nil , fmt .Errorf ("Wrong length for Serial Notify PDU: %d != 4" , len (toread ))
549
554
}
550
555
serial := binary .BigEndian .Uint32 (toread )
551
556
return & PDUSerialNotify {
@@ -555,7 +560,7 @@ func Decode(rdr io.Reader) (PDU, error) {
555
560
}, nil
556
561
case PDU_ID_SERIAL_QUERY :
557
562
if len (toread ) != 4 {
558
- return nil , errors . New ( fmt .Sprintf ("Wrong length for Serial Query PDU: %d != 4" , len (toread ) ))
563
+ return nil , fmt .Errorf ("Wrong length for Serial Query PDU: %d != 4" , len (toread ))
559
564
}
560
565
serial := binary .BigEndian .Uint32 (toread )
561
566
return & PDUSerialQuery {
@@ -565,22 +570,22 @@ func Decode(rdr io.Reader) (PDU, error) {
565
570
}, nil
566
571
case PDU_ID_RESET_QUERY :
567
572
if len (toread ) != 0 {
568
- return nil , errors . New ( fmt .Sprintf ("Wrong length for Reset Query PDU: %d != 0" , len (toread ) ))
573
+ return nil , fmt .Errorf ("Wrong length for Reset Query PDU: %d != 0" , len (toread ))
569
574
}
570
575
return & PDUResetQuery {
571
576
Version : pver ,
572
577
}, nil
573
578
case PDU_ID_CACHE_RESPONSE :
574
579
if len (toread ) != 0 {
575
- return nil , errors . New ( fmt .Sprintf ("Wrong length for Cache Response PDU: %d != 0" , len (toread ) ))
580
+ return nil , fmt .Errorf ("Wrong length for Cache Response PDU: %d != 0" , len (toread ))
576
581
}
577
582
return & PDUCacheResponse {
578
583
Version : pver ,
579
584
SessionId : sessionId ,
580
585
}, nil
581
586
case PDU_ID_IPV4_PREFIX :
582
587
if len (toread ) != 12 {
583
- return nil , errors . New ( fmt .Sprintf ("Wrong length for IPv4 Prefix PDU: %d != 12" , len (toread ) ))
588
+ return nil , fmt .Errorf ("Wrong length for IPv4 Prefix PDU: %d != 12" , len (toread ))
584
589
}
585
590
prefixLen := int (toread [1 ])
586
591
ip := toread [4 :8 ]
@@ -598,7 +603,7 @@ func Decode(rdr io.Reader) (PDU, error) {
598
603
}, nil
599
604
case PDU_ID_IPV6_PREFIX :
600
605
if len (toread ) != 24 {
601
- return nil , errors . New ( fmt .Sprintf ("Wrong length for IPv6 Prefix PDU: %d != 24" , len (toread ) ))
606
+ return nil , fmt .Errorf ("Wrong length for IPv6 Prefix PDU: %d != 24" , len (toread ))
602
607
}
603
608
prefixLen := int (toread [1 ])
604
609
ip := toread [4 :20 ]
@@ -616,7 +621,7 @@ func Decode(rdr io.Reader) (PDU, error) {
616
621
}, nil
617
622
case PDU_ID_END_OF_DATA :
618
623
if len (toread ) != 4 && len (toread ) != 16 {
619
- return nil , errors . New ( fmt .Sprintf ("Wrong length for End of Data PDU: %d != 4 or != 16" , len (toread ) ))
624
+ return nil , fmt .Errorf ("Wrong length for End of Data PDU: %d != 4 or != 16" , len (toread ))
620
625
}
621
626
622
627
var serial uint32
@@ -642,14 +647,14 @@ func Decode(rdr io.Reader) (PDU, error) {
642
647
}, nil
643
648
case PDU_ID_CACHE_RESET :
644
649
if len (toread ) != 0 {
645
- return nil , errors . New ( fmt .Sprintf ("Wrong length for Cache Reset PDU: %d != 0" , len (toread ) ))
650
+ return nil , fmt .Errorf ("Wrong length for Cache Reset PDU: %d != 0" , len (toread ))
646
651
}
647
652
return & PDUCacheReset {
648
653
Version : pver ,
649
654
}, nil
650
655
case PDU_ID_ROUTER_KEY :
651
656
if len (toread ) != 28 {
652
- return nil , errors . New ( fmt .Sprintf ("Wrong length for Router Key PDU: %d < 8" , len (toread ) ))
657
+ return nil , fmt .Errorf ("Wrong length for Router Key PDU: %d < 8" , len (toread ))
653
658
}
654
659
asn := binary .BigEndian .Uint32 (toread [20 :24 ])
655
660
spki := binary .BigEndian .Uint32 (toread [24 :28 ])
@@ -663,11 +668,19 @@ func Decode(rdr io.Reader) (PDU, error) {
663
668
}, nil
664
669
case PDU_ID_ERROR_REPORT :
665
670
if len (toread ) < 8 {
666
- return nil , errors . New ( fmt .Sprintf ("Wrong length for Error Report PDU: %d < 8" , len (toread ) ))
671
+ return nil , fmt .Errorf ("Wrong length for Error Report PDU: %d < 8" , len (toread ))
667
672
}
668
673
lenPdu := binary .BigEndian .Uint32 (toread [0 :4 ])
674
+ if len (toread ) < int (lenPdu )+ 8 {
675
+ return nil , fmt .Errorf ("Wrong length for Error Report PDU: %d < %d" , len (toread ), lenPdu + 4 )
676
+ }
669
677
errPdu := toread [4 : lenPdu + 4 ]
670
678
lenErrText := binary .BigEndian .Uint32 (toread [lenPdu + 4 : lenPdu + 8 ])
679
+ // int casting for each value is needed here to prevent an uint32 overflow that could result in
680
+ // upper bound being lower than lower bound causing a crash
681
+ if len (toread ) < int (lenPdu )+ 8 + int (lenErrText ) {
682
+ return nil , fmt .Errorf ("Wrong length for Error Report PDU: %d < %d" , len (toread ), lenPdu + 8 + lenErrText )
683
+ }
671
684
errMsg := string (toread [lenPdu + 8 : lenPdu + 8 + lenErrText ])
672
685
return & PDUErrorReport {
673
686
Version : pver ,
@@ -678,5 +691,4 @@ func Decode(rdr io.Reader) (PDU, error) {
678
691
default :
679
692
return nil , errors .New ("Could not decode packet" )
680
693
}
681
- return nil , nil
682
694
}
0 commit comments