Skip to content

Commit

Permalink
Fix possible uninitalized pointer access on unexpected array message …
Browse files Browse the repository at this point in the history
…data

When receiving multi-dimensional array data from the server, make sure
the dimensions are valid.

Fixes CVE-2020-17446.

Reported-by: Robert Scott <bugs@humanleg.org.uk>
  • Loading branch information
elprans committed Aug 9, 2020
1 parent 39040b3 commit 69bcdf5
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions asyncpg/protocol/codecs/array.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,21 @@ cdef inline array_decode(ConnectionSettings settings, FRBuffer *buf,
Codec elem_codec

if ndims == 0:
result = cpython.PyList_New(0)
return result
return []

if ndims > ARRAY_MAXDIM:
raise exceptions.ProtocolError(
'number of array dimensions ({}) exceed the maximum expected ({})'.
format(ndims, ARRAY_MAXDIM))
elif ndims < 0:
raise exceptions.ProtocolError(
'unexpected array dimensions value: {}'.format(ndims))

for i in range(ndims):
dims[i] = hton.unpack_int32(frb_read(buf, 4))
if dims[i] < 0:
raise exceptions.ProtocolError(
'unexpected array dimension size: {}'.format(dims[i]))
# Ignore the lower bound information
frb_read(buf, 4)

Expand Down Expand Up @@ -340,14 +345,18 @@ cdef _nested_array_decode(ConnectionSettings settings,
# An array of current positions at each array level.
int32_t indexes[ARRAY_MAXDIM]

if PG_DEBUG:
if ndims <= 0:
raise exceptions.ProtocolError(
'unexpected ndims value: {}'.format(ndims))

for i in range(ndims):
array_len *= dims[i]
indexes[i] = 0
strides[i] = NULL

if array_len == 0:
# A multidimensional array with a zero-sized dimension?
return []

elif array_len < 0:
# Array length overflow
raise exceptions.ProtocolError('array length overflow')

for i in range(array_len):
# Decode the element.
Expand Down

0 comments on commit 69bcdf5

Please sign in to comment.