From 9493d7b48c410f058b85552e1668b33f858afcac Mon Sep 17 00:00:00 2001 From: Evgeny Margolis Date: Mon, 13 Jun 2022 22:26:59 -0700 Subject: [PATCH] Fix Potential ASN1 Buffer Overflows in EnterContainer() and DecodeHead() (#19549) --- src/lib/asn1/ASN1Reader.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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);