Skip to content

Commit d7cb42d

Browse files
committed
Fix OverflowError from oversized BER length field (#100)
A certain payload with a length field >8 bytes produces an integer exceeding sys.maxsize, causing an unhandled OverflowError in BytesIO.read(). Reject such length fields early with MAX_LENGTH_OCTETS = 8. Fixes: #54
1 parent e7356f8 commit d7cb42d

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

‎pyasn1/codec/ber/decoder.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
# 20 octets allows up to 140-bit integers, supporting UUID-based OIDs
3838
MAX_OID_ARC_CONTINUATION_OCTETS = 20
3939

40+
# Maximum number of bytes in a BER length field (8 bytes = up to 2^64-1)
41+
MAX_LENGTH_OCTETS = 8
42+
4043

4144
class AbstractPayloadDecoder(object):
4245
protoComponent = None
@@ -1682,13 +1685,18 @@ def __call__(self, substrate, asn1Spec=None,
16821685

16831686
elif firstOctet > 128:
16841687
size = firstOctet & 0x7F
1688+
1689+
if size > MAX_LENGTH_OCTETS:
1690+
raise error.PyAsn1Error(
1691+
'BER length field size %d exceeds limit (%d)' % (
1692+
size, MAX_LENGTH_OCTETS)
1693+
)
1694+
16851695
# encoded in size bytes
16861696
for encodedLength in readFromStream(substrate, size, options):
16871697
if isinstance(encodedLength, SubstrateUnderrunError):
16881698
yield encodedLength
16891699
encodedLength = list(encodedLength)
1690-
# missing check on maximum size, which shouldn't be a
1691-
# problem, we can handle more than is possible
16921700
if len(encodedLength) != size:
16931701
raise error.SubstrateUnderrunError(
16941702
'%s<%s at %s' % (size, len(encodedLength), tagSet)

‎tests/codec/ber/test_decoder.py‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,6 +2117,61 @@ def testZipfileMany(self):
21172117
os.remove(path)
21182118

21192119

2120+
class LengthFieldLimitTestCase(BaseTestCase):
2121+
"""Test protection against oversized BER length fields."""
2122+
2123+
def testOversizedLengthField(self):
2124+
"""Length field exceeding MAX_LENGTH_OCTETS must raise PyAsn1Error."""
2125+
# 0x89 = long form, 9-byte length (exceeds 8-byte limit)
2126+
payload = b'\x02\x89' + b'\xFF' * 9 + b'\x01'
2127+
try:
2128+
decoder.decode(payload)
2129+
except error.PyAsn1Error:
2130+
pass
2131+
else:
2132+
assert False, 'Oversized length field not rejected'
2133+
2134+
def testNoOverflowError(self):
2135+
"""Must raise PyAsn1Error, not OverflowError."""
2136+
payload = b'\x02\x90' + b'\xFF' * 16 + b'\x01'
2137+
try:
2138+
decoder.decode(payload)
2139+
except error.PyAsn1Error:
2140+
pass
2141+
except OverflowError:
2142+
assert False, 'Got OverflowError instead of PyAsn1Error'
2143+
2144+
def testMaxAllowedLengthFieldWorks(self):
2145+
"""8-byte length field (the maximum allowed) must be accepted."""
2146+
# 0x88 = long form, 8-byte length, value = 5
2147+
payload = b'\x04\x88' + b'\x00' * 7 + b'\x05' + b'hello'
2148+
asn1Object, _ = decoder.decode(payload)
2149+
assert bytes(asn1Object) == b'hello', \
2150+
'Failed to decode with 8-byte length field'
2151+
2152+
def testNormalLengthFieldWorks(self):
2153+
"""Standard short-form and long-form lengths must still work."""
2154+
# Short form: length < 128
2155+
asn1Object, _ = decoder.decode(b'\x02\x01\x05')
2156+
assert asn1Object == 5
2157+
2158+
# Long form 1-byte: 0x81 + 1 byte length
2159+
payload = b'\x04\x81\x05' + b'hello'
2160+
asn1Object, _ = decoder.decode(payload)
2161+
assert bytes(asn1Object) == b'hello'
2162+
2163+
def testErrorMessageContainsLimit(self):
2164+
"""Error message must indicate the length field size limit."""
2165+
payload = b'\x02\x89' + b'\xFF' * 9 + b'\x01'
2166+
try:
2167+
decoder.decode(payload)
2168+
except error.PyAsn1Error as exc:
2169+
assert 'length field size' in str(exc).lower(), \
2170+
'Error message missing size info: %s' % exc
2171+
else:
2172+
assert False, 'Expected PyAsn1Error'
2173+
2174+
21202175
class NonStreamingCompatibilityTestCase(BaseTestCase):
21212176
def setUp(self):
21222177
from pyasn1 import debug

0 commit comments

Comments
 (0)