Skip to content

Commit 1800034

Browse files
andy31415andreilitvinrestyled-commits
authored andcommitted
Set a recursion depth limit for TLV (#26301)
* Set a recursion depth limit for TLV * Restyled by clang-format * Restyled by prettier-markdown --------- Co-authored-by: Andrei Litvin <andreilitvin@google.com> Co-authored-by: Restyled.io <commits@restyled.io>
1 parent c98fb28 commit 1800034

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

docs/ERROR_CODES.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This file was **AUTOMATICALLY** generated by
2121
| 2 | 0x02 | `CHIP_ERROR_CONNECTION_ABORTED` |
2222
| 3 | 0x03 | `CHIP_ERROR_INCORRECT_STATE` |
2323
| 4 | 0x04 | `CHIP_ERROR_MESSAGE_TOO_LONG` |
24+
| 5 | 0x05 | `CHIP_ERROR_RECURSION_DEPTH_LIMIT` |
2425
| 6 | 0x06 | `CHIP_ERROR_TOO_MANY_UNSOLICITED_MESSAGE_HANDLERS` |
2526
| 7 | 0x07 | `CHIP_ERROR_NO_UNSOLICITED_MESSAGE_HANDLER` |
2627
| 8 | 0x08 | `CHIP_ERROR_NO_CONNECTION_HANDLER` |

src/lib/core/CHIPError.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ bool FormatCHIPError(char * buf, uint16_t bufSize, CHIP_ERROR err)
7474
case CHIP_ERROR_MESSAGE_TOO_LONG.AsInteger():
7575
desc = "Message too long";
7676
break;
77+
case CHIP_ERROR_RECURSION_DEPTH_LIMIT.AsInteger():
78+
desc = "Recursion depth limit reached";
79+
break;
7780
case CHIP_ERROR_TOO_MANY_UNSOLICITED_MESSAGE_HANDLERS.AsInteger():
7881
desc = "Too many unsolicited message handlers";
7982
break;

src/lib/core/CHIPError.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,10 @@ using CHIP_ERROR = ::chip::ChipError;
466466
*/
467467
#define CHIP_ERROR_MESSAGE_TOO_LONG CHIP_CORE_ERROR(0x04)
468468

469-
// AVAILABLE: 0x05
469+
/**
470+
* Recursion depth overflow
471+
*/
472+
#define CHIP_ERROR_RECURSION_DEPTH_LIMIT CHIP_CORE_ERROR(0x05)
470473

471474
/**
472475
* @def CHIP_ERROR_TOO_MANY_UNSOLICITED_MESSAGE_HANDLERS

src/lib/core/TLVUtilities.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ namespace TLV {
3333

3434
namespace Utilities {
3535

36+
namespace {
37+
38+
// Sets up a limit on recursion depth, to avoid any stack overflows
39+
// on very deep TLV structures. Embedded has limited stack space.
40+
constexpr size_t kMaxRecursionDepth = 10;
41+
42+
} // namespace
43+
3644
struct FindContext
3745
{
3846
const Tag & mTag;
@@ -63,6 +71,11 @@ static CHIP_ERROR Iterate(TLVReader & aReader, size_t aDepth, IterateHandler aHa
6371
{
6472
CHIP_ERROR retval = CHIP_NO_ERROR;
6573

74+
if (aDepth >= kMaxRecursionDepth)
75+
{
76+
return CHIP_ERROR_RECURSION_DEPTH_LIMIT;
77+
}
78+
6679
if (aReader.GetType() == kTLVType_NotSpecified)
6780
{
6881
ReturnErrorOnFailure(aReader.Next());

src/lib/core/tests/TestCHIPErrorStr.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ static const CHIP_ERROR kTestElements[] =
5353
CHIP_ERROR_CONNECTION_ABORTED,
5454
CHIP_ERROR_INCORRECT_STATE,
5555
CHIP_ERROR_MESSAGE_TOO_LONG,
56+
CHIP_ERROR_RECURSION_DEPTH_LIMIT,
5657
CHIP_ERROR_TOO_MANY_UNSOLICITED_MESSAGE_HANDLERS,
5758
CHIP_ERROR_NO_UNSOLICITED_MESSAGE_HANDLER,
5859
CHIP_ERROR_NO_CONNECTION_HANDLER,

0 commit comments

Comments
 (0)