multiport: derive lane sessions on demand, install once authenticated

How many lanes a tunnel has is partly the peer's call: it advertises how many it
sends on and we have to be able to receive all of them, up to the header's 256.
Deriving them all when the handshake completes meant a peer advertising a large
count cost us a replay window and two cipher states per lane, per tunnel, for
lanes it may never send on.

Derive each session on the first packet that needs it instead. The TX side asks
through laneSet.session, which installs on the spot — we only ask for lanes we
chose to send on. The RX side can't do that: anyone who can spoof a tunnel's
local index can name any lane, and installing on sight would hand them the same
allocation for free. So laneSession hands back a session without publishing it
and reports that it did; outside.go installs it only once the packet has
decrypted, which is the first moment the lane is known to be real. A spoofer
gets an HKDF per packet and nothing retained.

Only the session table is sized by the peer's advert now. txAddr, demand and
probe are sized by the lanes we will actually send on, so the peer can no longer
size our per-lane tx state either.

Also refuse relayed lane packets before the session lookup rather than after,
so a junk relay packet can't reach the derivation path at all.
This commit is contained in:
Wade Simmons committed 2026-09-02 14:48:17 -04:00
1 parent fb20de39b2
commit 8c5c740571
6 files changed
+288 -103

No files matched your search

+10 -10
View File
@@ -77,8 +77,8 @@ func newConnectionStateFromResult(r *handshake.Result) (*ConnectionState, error)
return ci, nil
}
// newLaneConnectionState derives multiport lane s's session from the completed
// base handshake. Each key is an HKDF expansion of the base tunnel's matching
// newLaneConnectionState derives multiport lane s's session from the base
// tunnel's material. Each key is an HKDF expansion of the base tunnel's matching
// key, labelled with the lane index, so the pair stays matched with no extra
// negotiation: Noise leaves our send key equal to the peer's receive key, and
// expanding both with the same label preserves that.
@@ -86,26 +86,26 @@ func newConnectionStateFromResult(r *handshake.Result) (*ConnectionState, error)
// The lane gets its own counter and replay window starting from zero. No
// handshake messages were spent on it, so unlike the base session there is
// nothing to seed.
func newLaneConnectionState(r *handshake.Result, lane uint8) (*ConnectionState, error) {
func newLaneConnectionState(m *laneMaterial, lane uint8) (*ConnectionState, error) {
if lane == 0 {
return nil, fmt.Errorf("lane 0 is the base session")
}
eKey, err := deriveLaneKey(r.EKey.UnsafeKey(), lane)
eKey, err := deriveLaneKey(m.eKey, lane)
if err != nil {
return nil, err
}
dKey, err := deriveLaneKey(r.DKey.UnsafeKey(), lane)
dKey, err := deriveLaneKey(m.dKey, lane)
if err != nil {
return nil, err
}
return &ConnectionState{
myCert: r.MyCert,
initiator: r.Initiator,
peerCert: r.RemoteCert,
eKey: noiseutil.NewCipherStateFromKey(eKey, r.Cipher),
dKey: noiseutil.NewCipherStateFromKey(dKey, r.Cipher),
myCert: m.myCert,
initiator: m.initiator,
peerCert: m.peerCert,
eKey: noiseutil.NewCipherStateFromKey(eKey, m.cipher),
dKey: noiseutil.NewCipherStateFromKey(dKey, m.cipher),
window: NewBits(ReplayWindow),
epoch: sessionEpoch.Add(1),
}, nil