-
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
Fix CRMP resend null out retained buffer #7312
Conversation
59af2c3
to
c052e28
Compare
src/transport/SecureSessionMgr.h
Outdated
* 3. Encode the packet header and prepend it to message. | ||
* Returns a prepared message in preparedMessage. | ||
*/ | ||
CHIP_ERROR PrepareMessage(SecureSessionHandle session, PayloadHeader & payloadHeader, System::PacketBufferHandle && msgBuf, |
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.
The way I understand it, the method takes a buffer of data and encrypts its + adds the packet header. I find "prepare" a bit vague and it forces me to read through the comments to understand what the method does.
Is it correct that this is the one way we expect to convert a packet handle to an encrypted one? How about in this case:
EncryptPayload
or BuildEncryptedMessagePayload
and
SendEncryptedPayload
?
This way I do not read comments, I understand that giving a buffer, I encrypt it and send it.
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 API is also handling unsecure message in SessionEstablishmentExchangeDispatch.cpp, So it is not proper to name it BuildEncryptedMessagePayload
And BuildMessagePayload
is also a bit strange.
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.
The prepared steps in the comment say "it encrypts the msgBuf".
I am trying to find out if we can get a more descriptive name than "Prepare". From what I see this is that this has:
- Input: session, header, messagebuffer
- Output: EncryptedMessageBuffer
Which tells me it encrypts and adds a header. Realistically also wondering why we take a sessionhandle instead of just keys if we do encryption, but that is not for this PR.
Could you either update the comment about encryption or find some descriptive name (or both)?
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 think missing "override" made me not realize this particular implementation does "encrypt" but others may not. Please add override keyword here and below as needed and then I think the name is ok.
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.
Actually, this particular impl always encrypts. The message dispatch bits can keep Prepare(), etc, but we could name this API something different and clearer.
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.
The summary states that manual testing was done and I was about to comment about 'can we also have automated tests please?' However I also see test cases modified, so the code is exercised in unit tests as well.
Could we update the unit tests to be able to detect the bug to make sure it never happens again?
The logic causing the problem no longer exists, |
Size increase report for "gn_qpg6100-example-build" from f6af7b9
Full report output
|
src/transport/SecureSessionMgr.cpp
Outdated
if (state == nullptr) | ||
{ | ||
return CHIP_ERROR_NOT_CONNECTED; | ||
}; |
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: ";" not needed here
src/transport/SecureSessionMgr.h
Outdated
@@ -68,6 +68,8 @@ class EncryptedPacketBufferHandle final : private System::PacketBufferHandle | |||
|
|||
uint32_t GetMsgId() const; | |||
|
|||
PacketBufferHandle Retain() const { return PacketBufferHandle::Retain(); } |
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 is violating the main invariant of this class, which is that you have to call CastToWritable
to be able to modify it.
There are two options here:
- We make
CastToWritable
callable on an lvalue and have it do aRetain()
internally:PacketBufferHandle CastToWritable() { return PacketBufferHandle::Retain(); }
- We add a
Retain
that returnsEncryptedPacketBufferHandle
:EncryptedPacketBufferHandle Retain() { return EncryptedPacketBufferHandle(PacketBufferHandle::Retain()); }
and then weCastToWritable
that thing thatRetain
returns.
src/transport/SecureSessionMgr.h
Outdated
* 3. Encode the packet header and prepend it to message. | ||
* Returns a prepared message in preparedMessage. | ||
*/ | ||
CHIP_ERROR PrepareMessage(SecureSessionHandle session, PayloadHeader & payloadHeader, System::PacketBufferHandle && msgBuf, |
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.
Actually, this particular impl always encrypts. The message dispatch bits can keep Prepare(), etc, but we could name this API something different and clearer.
Size increase report for "nrfconnect-example-build" from 7d54157
Full report output
|
Size increase report for "esp32-example-build" from 7d54157
Full report output
|
/rebase |
@bzbarsky-apple I have upated a new patch to resolve your suggestion, please take a look |
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.
My apologies for the lag; I missed the comments being addressed. Thank you for the direct ping!
Co-authored-by: Boris Zbarsky <[email protected]>
/rebase |
* Fix CRMP resend null out retained buffer * Resolve comments * Apply suggestions from code review Co-authored-by: Boris Zbarsky <[email protected]> Co-authored-by: Boris Zbarsky <[email protected]>
Problem
There is a bug that resend message will null out CRMP retained buffer. Due to this line:
The code is a little strange, hard to understand, and error-prone.
Change overview
The PR split send message into 2 steps:
Then
SendMessage
becomes:ResendMessage
becomes:This makes all things clear and easy to understand.
Testing
Manually tested with echo and im