Skip to content
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 panic when peer is mid-handshake #2842

Merged

Conversation

jkczyz
Copy link
Contributor

@jkczyz jkczyz commented Jan 22, 2024

Peer::their_node_id is set to Some during the handshake process. However, df3ab2e accesses the field unconditionally, causing a panic. This may be triggered if a gossip message is received mid-handshake from another peer or if the user calls broadcast_node_announcement during this time. The latter tends to be executed on a timer.

Update the fuzz test to call broadcast_node_announcement mid-handshake on an inbound channel in order to trigger a panic. Ensure that Peer::their_node_id is only accessed once the handshake is complete.

Copy link

coderabbitai bot commented Jan 22, 2024

Walkthrough

The codebase has been updated with a new functionality in the fuzz testing suite and minor refactoring in the peer handler of the lightning module. The fuzz testing now includes a scenario for broadcasting node announcements, enhancing the robustness of the network communication features. Meanwhile, the lightning module has seen a reorganization of code for better readability, along with the addition of log initialization within certain conditions, although the core logic remains untouched.

Changes

File Path Change Summary
fuzz/src/full_stack.rs Added a new match arm for broadcasting node announcements in do_test function. Comments related to broadcasting node announcements have been added to the test module.
lightning/src/ln/peer_handler.rs Reordered code and added logger initialization within conditional blocks. The logic and control flow remain largely unchanged.

🐇✨
In the realm of code where the lightning plays,
A rabbit hopped through the algorithmic maze.
With a flip and a broadcast, the nodes announce,
"We're more robust!" the bytes joyfully pronounce. 📢🎉

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 6b0ba8c and 3dee0cf.
Files selected for processing (2)
  • fuzz/src/full_stack.rs (3 hunks)
  • lightning/src/ln/peer_handler.rs (3 hunks)
Files skipped from review due to trivial changes (1)
  • lightning/src/ln/peer_handler.rs
Additional comments: 1
fuzz/src/full_stack.rs (1)
  • 717-719: The addition of the match arm at line 17 to call broadcast_node_announcement during the handshake process is consistent with the PR's objective to replicate the panic scenario for the fuzz test.

@codecov-commenter
Copy link

codecov-commenter commented Jan 22, 2024

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (5592378) 88.54% compared to head (c7465bd) 88.53%.
Report is 2 commits behind head on main.

Files Patch % Lines
lightning/src/ln/peer_handler.rs 0.00% 3 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2842      +/-   ##
==========================================
- Coverage   88.54%   88.53%   -0.01%     
==========================================
  Files         115      115              
  Lines       92277    92277              
  Branches    92277    92277              
==========================================
- Hits        81706    81702       -4     
- Misses       8068     8073       +5     
+ Partials     2503     2502       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@TheBlueMatt
Copy link
Collaborator

Actually, can you drop the fuzz change commit? I have a larger patch locally that I want to upstream that adds broader coverage.

Peer::their_node_id is set to Some during the handshake process.
However, df3ab2e accesses the field
unconditionally, causing a panic. This may be triggered if a gossip
message is received mid-handshake from another peer or if the user calls
broadcast_node_announcement during this time. The latter tends to be
executed on a timer.

Ensure that Peer::their_node_id is only accessed once the handshake is
complete.
@jkczyz jkczyz force-pushed the 2024-01-fix-peer-handler-unwrap branch from 3dee0cf to c7465bd Compare January 22, 2024 21:36
@TheBlueMatt TheBlueMatt added this to the 0.0.122 milestone Jan 22, 2024
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 6b0ba8c and c7465bd.
Files selected for processing (1)
  • lightning/src/ln/peer_handler.rs (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • lightning/src/ln/peer_handler.rs

@TheBlueMatt TheBlueMatt modified the milestones: 0.0.122, 0.0.121 Jan 22, 2024
if !peer.handshake_complete() ||
!peer.should_forward_channel_announcement(msg.contents.short_channel_id) {
continue
}
debug_assert!(peer.their_node_id.is_some());
debug_assert!(peer.channel_encryptor.is_ready_for_encryption());
let logger = WithContext::from(&self.logger, Some(peer.their_node_id.unwrap().0), None);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also remove the unwrap while we're here?

Suggested change
let logger = WithContext::from(&self.logger, Some(peer.their_node_id.unwrap().0), None);
let logger = WithContext::from(&self.logger, peer.their_node_id.map(|p| p.0), None);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here and few other places

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between c7465bd and 7b8d018.
Files selected for processing (1)
  • lightning/src/ln/peer_handler.rs (6 hunks)
Files skipped from review as they are similar to previous changes (1)
  • lightning/src/ln/peer_handler.rs

Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super straightforward patch, only real change is fewer unwraps. Gonna go ahead and land this so we can get to cuting.

@TheBlueMatt TheBlueMatt merged commit 444740c into lightningdevkit:main Jan 22, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants