Skip to content

Commit cd1f389

Browse files
sean-jackc
authored andcommitted
pgtype: bound array binary decode element length against remaining bytes
ArrayCodec.decodeBinary looped elementCount times reading binary.BigEndian.Uint32(src[rp:]) then slicing src[rp:rp+elemLen] without checking that rp+4 <= len(src) nor rp+elemLen <= len(src). A server returning an array header claiming more elements than the body holds, or a per-element length past the end, panicked the client. Check remaining bytes before each Uint32 read and before each slice. Reported by CrowdStrike (F06).
1 parent ff27b5b commit cd1f389

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

‎pgtype/array_codec.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,17 @@ func (c *ArrayCodec) decodeBinary(m *Map, arrayOID uint32, src []byte, array Arr
283283
}
284284

285285
for i := range elementCount {
286+
if len(src[rp:]) < 4 {
287+
return fmt.Errorf("array body truncated at element %d", i)
288+
}
286289
elem := array.ScanIndex(i)
287290
elemLen := int(int32(binary.BigEndian.Uint32(src[rp:])))
288291
rp += 4
289292
var elemSrc []byte
290293
if elemLen >= 0 {
294+
if len(src[rp:]) < elemLen {
295+
return fmt.Errorf("array element %d length %d exceeds remaining %d bytes", i, elemLen, len(src[rp:]))
296+
}
291297
elemSrc = src[rp : rp+elemLen]
292298
rp += elemLen
293299
}

0 commit comments

Comments
 (0)