From 9ee7e522b1045b9f097cf9011263e8d2074e8b23 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sun, 4 Feb 2018 11:04:29 +0100 Subject: [PATCH] Proper stamps length check --- dnscrypt-proxy/stamps.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/dnscrypt-proxy/stamps.go b/dnscrypt-proxy/stamps.go index 7fe51aaf..21c12af5 100644 --- a/dnscrypt-proxy/stamps.go +++ b/dnscrypt-proxy/stamps.go @@ -85,15 +85,15 @@ func NewServerStampFromString(stampStr string) (ServerStamp, error) { func newDNSCryptServerStamp(bin []byte) (ServerStamp, error) { stamp := ServerStamp{proto: StampProtoTypeDNSCrypt} - if len(bin) < 24 { - return stamp, errors.New("Stamp is too short") + if len(bin) < 66 { + return stamp, fmt.Errorf("Stamp is too short", bin) } stamp.props = ServerInformalProperties(binary.LittleEndian.Uint64(bin[1:9])) binLen := len(bin) pos := 9 len := int(bin[pos]) - if len >= binLen-pos { + if 1+len >= binLen-pos { return stamp, errors.New("Invalid stamp") } pos++ @@ -104,7 +104,7 @@ func newDNSCryptServerStamp(bin []byte) (ServerStamp, error) { } len = int(bin[pos]) - if len >= binLen-pos { + if 1+len >= binLen-pos { return stamp, errors.New("Invalid stamp") } pos++ @@ -129,13 +129,15 @@ func newDNSCryptServerStamp(bin []byte) (ServerStamp, error) { func newDoHServerStamp(bin []byte) (ServerStamp, error) { stamp := ServerStamp{proto: StampProtoTypeDoH, hashes: [][]byte{}} - + if len(bin) < 22 { + return stamp, fmt.Errorf("Stamp is too short", bin) + } stamp.props = ServerInformalProperties(binary.LittleEndian.Uint64(bin[1:9])) binLen := len(bin) pos := 9 len := int(bin[pos]) - if len >= binLen-pos { + if 1+len >= binLen-pos { return stamp, errors.New("Invalid stamp") } pos++ @@ -148,7 +150,7 @@ func newDoHServerStamp(bin []byte) (ServerStamp, error) { for { vlen := int(bin[pos]) len = vlen & ^0x80 - if len >= binLen-pos { + if 1+len >= binLen-pos { return stamp, errors.New("Invalid stamp") } pos++ @@ -160,7 +162,7 @@ func newDoHServerStamp(bin []byte) (ServerStamp, error) { } len = int(bin[pos]) - if len >= binLen-pos { + if 1+len >= binLen-pos { return stamp, errors.New("Invalid stamp") } pos++