-
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
Prevent TLVWrite from possibly performing buffer overrun if trying to reserve #30714
Prevent TLVWrite from possibly performing buffer overrun if trying to reserve #30714
Conversation
PR #30714: Size comparison from b379158 to 21e888d Increases (72 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, cyw30739, efr32, esp32, k32w, linux, mbed, nrfconnect, psoc6, qpg, telink)
Decreases (2 builds for linux)
Full report (72 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, cyw30739, efr32, esp32, k32w, linux, mbed, nrfconnect, psoc6, qpg, telink)
|
I think the real overflow bug is that |
This shouldn't be the case according to the documentation of the |
You are right, I have remove that section now. Sorry about that |
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.
Thank you. As a followup: we should document what the mRemainingLen and mMaxLen members mean... But followup, not this PR.
PR #30714: Size comparison from b6e465a to 190c324 Increases (59 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, efr32, esp32, linux, mbed, nrfconnect, psoc6, telink)
Decreases (39 builds for bl602, bl702, bl702l, cc13x4_26x4, efr32, esp32, linux, nrfconnect, qpg, telink)
Full report (72 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, cyw30739, efr32, esp32, k32w, linux, mbed, nrfconnect, psoc6, qpg, telink)
|
PR #30714: Size comparison from b6e465a to 7fb1c88 Increases (59 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, efr32, esp32, linux, mbed, nrfconnect, psoc6, telink)
Decreases (39 builds for bl602, bl702, bl702l, cc13x4_26x4, efr32, esp32, linux, nrfconnect, qpg, telink)
Full report (72 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, cyw30739, efr32, esp32, k32w, linux, mbed, nrfconnect, psoc6, qpg, telink)
|
Problem:
TLVWriter::WriteData
may callGetNewBuffer
to try getting more buffers for TLVWriter to continue writing large amounts of data into. When this happens inTLVWriter::WriteData
, we do not properly account for the reserved memory. As a result, after getting the new buffer and callingUnreserveBuffer
the Writer can write into memory beyond what was allocated resulting in buffer overrun.TLVWriter::WriteData
were to try accounting for the reserved data, you would first need to fill in the rest of the buffer that is supposed to be reserved. After that you would be relying on 2 things that are not guaranteed. First thatGetNewBuffer
is successful. Secondly, if successful it is possible that the new buffer's size is smaller than what you had reserved.Alternative solution considered:
ReserveBuffer
taking away frommRemainingLen
you take away frommMaxLen
.TLVWriter::ReserveBuffer
. As of today everything that callsTLVWriter::ReserveBuffer
is usingSystem::PacketBufferHandle::New(chip::app::kMaxSecureSduLengthBytes);
. This means that theTLVBackingStore
for the TLVWriter that isTLVPacketBufferBackingStore
. For this particular caseuseChainBuffers == false
.PacketBufferHandle
will reserve the header for us, but doesn't reserve anything at the end which we are trying to do for closing TLV containers. In this casemRemainingLen != mMaxLen
, andmMaxLen
is not to be relied on to convey anything meaningful.Alternative not full explored, but might be better:
GetNewBuffer
fails until you call it, I am unsure how we could properly undo what you have written previously inTLVWriter::WriteData
. Just because I don't know right now how to "rollback" doesn't me this solution doesn't exist.System::PacketBufferHandle::New(chip::app::kMaxSecureSduLengthBytes);
whymMaxLen
cannot bemRemainingLen
. If someone know why any thinks this is a viable option I can explore it more.Why things are implemented the way they are right now:
TLVWriter::ReserveBuffer
as we have always set useChainedBuffers to false. In order to keep usage safe I am trying to make sure what we are currently doing today is allowed why stopping incorrect possible usage.ReserveBuffer
similarly to how it is used in the SDK today, but didn't want to set more precedent of using it without closing out possible footgun for usages different then what is in the SDK today.Tests:
TLVWriter::ReserveBuffer