From 1a0a3dc5e4816fcf3d14e7bc2bb2bd7bfae36f8f Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Thu, 28 May 2020 18:48:45 +0000 Subject: [PATCH] rpcserver: Avoid panic during hash decode The input hex string must be exactly 64 bytes, else an error will be returned. The input validation previously checked that the length following decoding was correct, but was missing a check that the destination slice was not smaller than half the source's length. Moving the length check first keeps the existing short source check while also preventing a panic with a long input string. --- rpcserver.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/rpcserver.go b/rpcserver.go index eb256a5e..8b9c1741 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -1471,8 +1471,11 @@ func handleExistsAddresses(_ context.Context, s *rpcServer, cmd interface{}) (in func decodeHashes(strs []string) ([]chainhash.Hash, error) { hashes := make([]chainhash.Hash, len(strs)) for i, s := range strs { - l, err := hex.Decode(hashes[i][:], []byte(s)) - if err != nil || l != 32 { + if len(s) != 2*chainhash.HashSize { + return nil, rpcDecodeHexError(s) + } + _, err := hex.Decode(hashes[i][:], []byte(s)) + if err != nil { return nil, rpcDecodeHexError(s) } // unreverse hash string bytes