Skip to content

ts_tunnel: add replay protection for incoming packets#179

Open
danderson wants to merge 1 commit into
mainfrom
push-osylulpsrzsr
Open

ts_tunnel: add replay protection for incoming packets#179
danderson wants to merge 1 commit into
mainfrom
push-osylulpsrzsr

Conversation

@danderson
Copy link
Copy Markdown
Member

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.

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
Copy link
Copy Markdown
Collaborator

@npry npry left a comment

Choose a reason for hiding this comment

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

lgtm with the panic doc, the other comments are nonblocking

Comment thread ts_tunnel/src/replay.rs
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, "}}")
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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()
}

Comment thread ts_tunnel/src/replay.rs
///
/// # Panics
///
/// If [`ReplayWindow::check(counter)`] is false.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Also document the previously-set case

Comment thread ts_tunnel/src/endpoint.rs
None(Queue),
/// Active session available.
Active {
recv: ReceiveSession,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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?

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.

ts_tunnel: implement packet replay protection

2 participants