ts_tunnel: add replay protection for incoming packets#179
Open
danderson wants to merge 1 commit into
Open
Conversation
Rejects packets for an established session that have excessively old counter values. To allow for some packet reordering on the wire, we maintain a sliding window of acceptable counter values, and track already received counter values in that window with a bit ring. Fixes #17 Signed-off-by: David Anderson <danderson@tailscale.com> Change-Id: Ib71e5ea78078bb2e84314bc0f2fee94e6a6a6964
npry
approved these changes
May 13, 2026
Collaborator
npry
left a comment
There was a problem hiding this comment.
lgtm with the panic doc, the other comments are nonblocking
Comment on lines
+87
to
+98
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| writeln!(f, "ReplayWindow{{\n last: {}\n bits:", self.last)?; | ||
| for block in self.blocks { | ||
| write!(f, " ")?; | ||
| for octet in 0usize..8 { | ||
| let v = ((block >> (octet * 8)) & 0xff) as u8; | ||
| write!(f, "{:08b} ", v.reverse_bits())?; | ||
| } | ||
| writeln!(f)?; | ||
| } | ||
| write!(f, "}}") | ||
| } |
Collaborator
There was a problem hiding this comment.
Nit: I would typically use Formatter::debug_struct here, since it handles and forwards additional formatting options automatically — you can get the indented pretty-printed repr with the {:#?} specifier:
Suggested change
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
| writeln!(f, "ReplayWindow{{\n last: {}\n bits:", self.last)?; | |
| for block in self.blocks { | |
| write!(f, " ")?; | |
| for octet in 0usize..8 { | |
| let v = ((block >> (octet * 8)) & 0xff) as u8; | |
| write!(f, "{:08b} ", v.reverse_bits())?; | |
| } | |
| writeln!(f)?; | |
| } | |
| write!(f, "}}") | |
| } | |
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
| struct BlockFormatter<'b>(&'b [u8]); | |
| impl Debug for BlockFormatter { | |
| fn fmt(&self, f: &mut std::fmt::Formatter<'_> -> std::fmt::Result { | |
| for b in self.0 { | |
| f.write("{:08b} ", b.reverse_bits())?; | |
| } | |
| Ok(()) | |
| } | |
| } | |
| f.debug_struct("ReplayWindow") | |
| .field("last", &self.last) | |
| .field("bits", &BitsFormatter(&self.blocks)) | |
| .finish() | |
| } |
| /// | ||
| /// # Panics | ||
| /// | ||
| /// If [`ReplayWindow::check(counter)`] is false. |
Collaborator
There was a problem hiding this comment.
Also document the previously-set case
| None(Queue), | ||
| /// Active session available. | ||
| Active { | ||
| recv: ReceiveSession, |
Collaborator
There was a problem hiding this comment.
Is the Box because the session size is large due to the contained bitset? afaik this lives on the heap anyway — is there a copy/clone somewhere that would otherwise be expensive?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rejects packets for an established session that have excessively old counter values. To allow for some packet reordering on the wire, we maintain a sliding window of acceptable counter values, and track already received counter values in that window with a bit ring.
Fixes #33
Change-Id: Ib71e5ea78078bb2e84314bc0f2fee94e6a6a6964
Adding the replay window made the receive session state a fair bit larger, which tripped a clippy check for unbalanced enum variants. I dealt with this by boxing the receive sessions, but it feels a little ad-hoc to me. Suggestions on better layouts welcome.