-
Notifications
You must be signed in to change notification settings - Fork 414
Fix encryption of broadcasted gossip messages #1715
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 encryption of broadcasted gossip messages #1715
Conversation
0097784
to
72e53cd
Compare
Codecov Report
@@ Coverage Diff @@
## main #1715 +/- ##
==========================================
- Coverage 90.84% 90.82% -0.03%
==========================================
Files 86 86
Lines 46448 46479 +31
Branches 46448 46479 +31
==========================================
+ Hits 42198 42213 +15
- Misses 4250 4266 +16
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
72e53cd
to
5976793
Compare
In 47e818f, forwarding broadcasted gossip messages was split into a separate per-peer message buffer. However, both it and the original regular-message queue are encrypted immediately when the messages are enqueued. Because the lightning P2P encryption algorithm is order-dependent, this causes messages to fail their MAC checks as the messages from the two queues may not be sent to peers in the order in which they were encrypted. The fix is to simply queue broadcast gossip messages unencrypted, encrypting them when we add them to the regular outbound buffer.
For non-gossip-broadcast messages, our current flow is to first serialize the message into a `Vec`, and then allocate a new `Vec` into which we write the encrypted+MAC'd message and header. This is somewhat wasteful, and its rather simple to instead allocate only one buffer and encrypt the message in-place.
Squashed without further changes. |
cd45baf
to
45ec1db
Compare
/// Encrypts the given message, returning the encrypted version. | ||
/// panics if the length of `message`, once encoded, is greater than 65535 or if the Noise | ||
/// handshake has not finished. | ||
pub fn encrypt_message<M: wire::Type>(&mut self, message: &M) -> Vec<u8> { |
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.
Could we consolidate this with encrypt_buffer
if we made gossip_broadcast_buffer
contain messages instead of encoded messages?
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.
We could, but I'm not sure that's better - many messages have multiple heap-allocated structs, and we'd have to clone all of that. The nice thing about storing the encoded message is we just need one heap-allocated Vec per peer, rather than potentially many.
In 47e818f, forwarding broadcasted
gossip messages was split into a separate per-peer message buffer.
However, both it and the original regular-message queue are
encrypted immediately when the messages are enqueued. Because the
lightning P2P encryption algorithm is order-dependent, this causes
messages to fail their MAC checks as the messages from the two
queues may not be sent to peers in the order in which they were
encrypted.
The fix is to simply queue broadcast gossip messages unencrypted,
encrypting them when we add them to the regular outbound buffer.
We also take this opportunity to optimize non-gossip message sending by avoiding allocating two buffers.