Rejecting replayed messages
The previous post listed three things still open: nonce reuse across restarts, no forward secrecy, and no replay protection. The first two need a handshake. The third doesn’t, so this pull request fixes it first.
The problem
Every message is authenticated, so nobody can forge or modify one. But nothing stopped someone from capturing a real message and sending it again. It still has a valid tag, so Open decrypted it and govpn wrote the packet to the TUN device a second time. Replaying a TCP segment usually does nothing, but replaying a UDP packet can repeat whatever it asked for.
Why not just keep the highest counter
Each message already carries its counter in the header, so the obvious fix is to remember the highest counter received and drop anything that isn’t above it. That breaks on a real network. UDP doesn’t guarantee order, so packet 12 can arrive after packet 13, and with that rule it would be dropped even though it’s new.
The fix is to remember a window of recent counters instead of just one. WireGuard does this, following RFC 6479, with a much bigger window than mine.
A window of 64 counters
replayWindow keeps the highest counter accepted so far and a 64-bit bitmap. Bit i is set if counter max - i was accepted:
type replayWindow struct {
mutex sync.Mutex
max uint64
bitmap uint64
}
There are three cases when a counter arrives:
- Above
max: it’s new. The bitmap shifts left by the difference, so the old bits move to match the newmax, and bit 0 is set. A jump of 64 or more clears the bitmap, since nothing old is still in range. - At or below
max, within 64: it’s accepted only if its bit isn’t set yet, and then the bit is set. - More than 63 below
max: it’s dropped. The window can’t tell whether it was seen, so it assumes it was.
if counter > w.max {
offset := counter - w.max
if offset >= windowSize {
w.bitmap = 0
} else {
w.bitmap = w.bitmap << offset
}
w.bitmap = w.bitmap | 1
w.max = counter
} else {
offset := w.max - counter
if offset >= windowSize {
return ErrReplay
}
if ((1 << offset) & w.bitmap) != 0 {
return ErrReplay
}
w.bitmap = w.bitmap | (1 << offset)
}
64 is small, but one uint64 makes the bitmap easy to follow. A message that arrives more than 63 packets late is dropped, and TCP inside the tunnel will retransmit it.
The zero value works: a new window starts with max at 0 and an empty bitmap, so counter 0 is accepted once, the same as any other.
Authenticate first, then mark
The order inside Open matters. The counter is in the header, in plaintext, and anyone can send a datagram with counter 2^40. If the window moved before the tag was checked, that one forged message would push max far ahead, and every real message after it would be dropped as too old. One packet would be enough to cut the tunnel.
So Open authenticates first and only then marks the counter:
res, err := s.recvAEAD.Open(dst, nonce[:], msg[HeaderSize:], nil)
if err != nil {
return nil, ErrAuth
}
if s.replayWindow.accept(counter) != nil {
return nil, ErrReplay
}
There’s a test that does exactly that attack: it sends a message with a forged high counter, checks that it fails with ErrAuth, and then checks that a real message still opens.
A replayed message is dropped like any other bad message, silently. The new error, ErrReplay, is only there so the tests can tell the cases apart.
Testing the window
Most of the tests are lists of steps: a counter and whether it should be accepted. They cover reordering, duplicates, the oldest counter that still fits in the window, the first one that doesn’t, jumps of exactly 63 and 64, and the counter limit from the encryption post. One case checks that a rejected counter leaves the window alone, since a rejection that moved max would make every later answer wrong.
main only opens messages from one goroutine today, but Session is documented as safe for concurrent use, so accept takes a mutex. Two tests start 100 goroutines that all try to accept the same counter at once, one against the window and one through Open, and check that exactly one succeeds. They run with -race in CI.
A side effect on restarts
This change makes restarts worse. When one peer restarts, its send counter goes back to 0, but the other peer still remembers the counters it received. Everything the restarted peer sends is now too old or already seen, so it’s dropped, until its counter passes the last one received before the restart. After a long session that can take a very long time.
Before, a restart quietly reused nonces. Now it also stalls the tunnel, which at least makes the problem visible. The README says to restart both peers to recover.
What’s still open
Nonces are still reused after a restart, and if both or neither peer pass -initiator. There is still no forward secrecy. All of that waits for the handshake, which will give each start of the tunnel fresh keys and a new counter, and fix the restart stall with it.
What’s next
Key pairs. The handshake needs each peer to have its own identity, so that’s the first piece.