Skip to content

Commit 69bcdf5

Browse files
committed
Fix possible uninitalized pointer access on unexpected array message data
When receiving multi-dimensional array data from the server, make sure the dimensions are valid. Fixes CVE-2020-17446. Reported-by: Robert Scott <[email protected]>
1 parent 39040b3 commit 69bcdf5

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

asyncpg/protocol/codecs/array.pyx

+16-7
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,21 @@ cdef inline array_decode(ConnectionSettings settings, FRBuffer *buf,
286286
Codec elem_codec
287287

288288
if ndims == 0:
289-
result = cpython.PyList_New(0)
290-
return result
289+
return []
291290

292291
if ndims > ARRAY_MAXDIM:
293292
raise exceptions.ProtocolError(
294293
'number of array dimensions ({}) exceed the maximum expected ({})'.
295294
format(ndims, ARRAY_MAXDIM))
295+
elif ndims < 0:
296+
raise exceptions.ProtocolError(
297+
'unexpected array dimensions value: {}'.format(ndims))
296298

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

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

343-
if PG_DEBUG:
344-
if ndims <= 0:
345-
raise exceptions.ProtocolError(
346-
'unexpected ndims value: {}'.format(ndims))
347-
348348
for i in range(ndims):
349349
array_len *= dims[i]
350350
indexes[i] = 0
351+
strides[i] = NULL
352+
353+
if array_len == 0:
354+
# A multidimensional array with a zero-sized dimension?
355+
return []
356+
357+
elif array_len < 0:
358+
# Array length overflow
359+
raise exceptions.ProtocolError('array length overflow')
351360

352361
for i in range(array_len):
353362
# Decode the element.

0 commit comments

Comments
 (0)