Skip to content

Commit b28e65b

Browse files
sean-jackc
authored andcommitted
pgtype: bound array element count against remaining message bytes
cardinality() returns 0 on negative overflow, but on a 64-bit platform int32 dimension lengths can produce a positive product that wraps int64 unpredictably; e.g. dims {2147483647, 2} → 4294967294, positive and fitting in int64, passed straight to make([]T, ...) inside SetDimensions. The header for this is 28 bytes. Bound elementCount by len(src[rp:]) / 4 (each element carries at minimum a 4-byte length header) before SetDimensions. This catches both the overflow case and grossly over-claimed dimensions, and ties the cap to the actual message body size rather than a fixed ceiling. Reported by CrowdStrike (F07).
1 parent cd1f389 commit b28e65b

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

‎pgtype/array_codec.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,19 @@ func (c *ArrayCodec) decodeBinary(m *Map, arrayOID uint32, src []byte, array Arr
267267
return err
268268
}
269269

270+
elementCount := cardinality(arrayHeader.Dimensions)
271+
// Each element carries at minimum a 4-byte length header, so elementCount cannot exceed the
272+
// remaining bytes / 4. This bounds the allocation in SetDimensions and the loop below against a
273+
// malicious server claiming huge dimensions in a small message.
274+
if maxElements := len(src[rp:]) / 4; elementCount > maxElements {
275+
return fmt.Errorf("array claims %d elements but only %d bytes remain", elementCount, len(src[rp:]))
276+
}
277+
270278
err = array.SetDimensions(arrayHeader.Dimensions)
271279
if err != nil {
272280
return err
273281
}
274282

275-
elementCount := cardinality(arrayHeader.Dimensions)
276283
if elementCount == 0 {
277284
return nil
278285
}

0 commit comments

Comments
 (0)