diff --git a/src/lib/asn1/ASN1Reader.cpp b/src/lib/asn1/ASN1Reader.cpp index 9309dc53448721..6ac0605149d0fc 100644 --- a/src/lib/asn1/ASN1Reader.cpp +++ b/src/lib/asn1/ASN1Reader.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2020-2021 Project CHIP Authors + * Copyright (c) 2020-2022 Project CHIP Authors * Copyright (c) 2013-2017 Nest Labs, Inc. * All rights reserved. * @@ -113,6 +113,8 @@ CHIP_ERROR ASN1Reader::EnterContainer(uint32_t offset) mElemStart = Value + offset; if (!IndefiniteLen) { + VerifyOrReturnError(CanCastTo(mBufEnd - Value), ASN1_ERROR_VALUE_OVERFLOW); + VerifyOrReturnError(static_cast(mBufEnd - Value) >= ValueLen, ASN1_ERROR_VALUE_OVERFLOW); mContainerEnd = Value + ValueLen; } @@ -303,8 +305,9 @@ CHIP_ERROR ASN1Reader::DecodeHead() IndefiniteLen = false; } + VerifyOrReturnError(CanCastTo(mBufEnd - p), ASN1_ERROR_VALUE_OVERFLOW); + VerifyOrReturnError(static_cast(mBufEnd - p) >= ValueLen, ASN1_ERROR_VALUE_OVERFLOW); VerifyOrReturnError(CanCastTo(p - mElemStart), ASN1_ERROR_VALUE_OVERFLOW); - mHeadLen = static_cast(p - mElemStart); EndOfContents = (Class == kASN1TagClass_Universal && Tag == 0 && !Constructed && ValueLen == 0);