-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add verification of the range of integer values for TLVReader -- 2nd attempt #5764
Conversation
Size increase report for "esp32-example-build" from f3ec8e6
Full report output
|
Size increase report for "nrfconnect-example-build" from f3ec8e6
Full report output
|
return err; | ||
} | ||
|
||
CHIP_ERROR TLVReader::_Get(uint64_t & v) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: wouldn't having two methods for signed and unsigned types, returning int64_t
and uint64_t
, respectively, result in a simpler code?
src/lib/core/CHIPTLV.h
Outdated
template<typename int_type> | ||
bool TLVTypeValid(uint64_t val64) const; | ||
CHIP_ERROR _Get(uint64_t & v); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These need to be documented.
|
||
CHIP_ERROR err = _Get(v64); | ||
ReturnErrorCodeIf(!TLVTypeValid<int8_t>(v64), CHIP_ERROR_WRONG_TLV_TYPE); | ||
v = CastToSigned(static_cast<uint8_t>(v64)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
v = CastToSigned(static_cast<uint8_t>(v64)); | |
v = static_cast<int8_t>(CastToSigned(v64)); |
is clearer. But even clearer would be to have an overload of _Get
that takes int64_t
and checks for the SignedInteger() type before returning success. Then here we could just CanCastTo
and return with a cast...
Similar comments for all the signed types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is still unaddressed.
TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast<int32_t>(INT8_MIN)); | ||
TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast<int64_t>(INT8_MIN)); | ||
|
||
TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast<uint8_t>(INT8_MIN), CHIP_ERROR_WRONG_TLV_TYPE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably check that INT8_MAX can't be read as uint8_t (below), since that one would actually be in range.
TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast<int16_t>(INT8_MAX)); | ||
TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast<int32_t>(INT8_MAX)); | ||
TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast<int64_t>(INT8_MAX)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably check that can't read as uint64_t, and same for all the signed max values.
|
||
TestNext<TLVReader>(inSuite, reader); | ||
|
||
TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast<int8_t>(INT16_MIN), CHIP_ERROR_WRONG_TLV_TYPE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that cast is undefined behavior... Should use 0 instead of INT16_MIN there, perhaps? Similar for the other to-signed narrowing casts.
@@ -485,11 +639,11 @@ void ReadEncoding1(nlTestSuite * inSuite, TLVReader & reader) | |||
|
|||
TestNext<TLVReader>(inSuite, reader2); | |||
|
|||
TestGet<TLVReader, bool>(inSuite, reader2, kTLVType_Boolean, ProfileTag(TestProfile_1, 2), true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why all the changes away from using TestGet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All Test*()
inline functions in this file have the same issue: they are functions, and, therefore, the assertion macros in those functions get expended before the functions are called/inlined. As the result, shown locations of assertion failures have nothing to do with the locations of actual problems.
The reason I have not completely removed TestGet
was that for some reason TEST_GET
macro had a problem with float/double types -- still need to figure out what is wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the result, shown locations of assertion failures have nothing to do with the locations of actual problems.
Ah, right. Would have been handy to put that in the commit message. ;)
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
@bitkis Any update on the PR feedback? |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
1 similar comment
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per the updated template, can you update the PR here?
#### Problem
What is being fixed? Examples:
* Fix crash on startup
* Fixes #12345 12345 Frobnozzle is leaky (exactly like that, so GitHub will auto-close the issue).
#### Change overview
What's in this PR
#### Testing
How was this tested? (at least one bullet point required)
• If unit tests were added, how do they cover this issue?
• If unit tests existed, how were they fixed/modified to prevent this in future?
• If integration tests were added, how do they verify this change?
• If manually tested, what platforms controller and device platforms were manually tested, and how?
• If no testing is required, why not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still need to address issues raised in #5764 (review)
bool TLVTypeValid(uint64_t val64) const; | ||
CHIP_ERROR _Get(uint64_t & v); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These still need to be documented.
|
||
CHIP_ERROR err = _Get(v64); | ||
ReturnErrorCodeIf(!TLVTypeValid<int8_t>(v64), CHIP_ERROR_WRONG_TLV_TYPE); | ||
v = CastToSigned(static_cast<uint8_t>(v64)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is still unaddressed.
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
This stale pull request has been automatically closed. Thank you for your contributions. |
… unexpected values. Two changes: 1) Ensure the value was encoded with the signed-ness the reader expects. 2) Ensure the value is in the range the reader expects instad of ending up with overflow and whatever that does.. Fixes project-chip#5152 The test changes were largely lifted from project-chip#5764
Problem
Currently, TVLReader's method
Get
does not validate sizes of requested integers. In case when the size is less than size of TVL field, incorrect value is returned to caller; no error condition is indicated.Summary of Changes
Size validation is added, error conditions are handled properly.
Fixes #5152
[see https://github.com/CHIP-Specifications/connectedhomeip-spec/issues/2115 and https://github.com//pull/5286 discussions]