lx: fix transport padding buffer overrun + harden AWG config guards

Transport padding (s4) crashed the whole process with
"index out of range" in RoutineSequentialSender on the first data
packet: InputPacket/InputPackets sized elem.buffer without headroom
for the in-buffer right-shift that prepends the random prefix.

- send.go: reserve paddings.transport in both injection-path
  allocLength computations; replace the manual backward byte loop
  with an overlap-safe copy; defensively grow the buffer (pool-backed)
  if it still lacks headroom, dropping packets that cannot fit a
  single WG message instead of overrunning.
- receive.go: drop the rxBytes/timers block duplicated by the AWG
  re-graft (rx accounting was doubled, keepKeyFreshReceiving fired
  twice per batch).
- send.go: swap jmin/jmax when configured inverted (UAPI validates
  the fields only individually; a swapped pair panicked rand.Int
  with a non-positive bound on the first handshake).
- obf*.go: bound obfuscator length args to [0, MaxMessageSize]
  (negative panicked slice bounds, huge ones OOMed the handshake).
- magic-header.go: widen to int64 before end-start+1 so a full-range
  header cannot wrap to a zero rand.Int bound.

Tests: transport_padding_test.go reproduces the on-device crash
byte-for-byte (red on the previous commit, green now) across both
injection paths and the tun path; obf_guards_test.go pins the
config-value guards.
This commit is contained in:
Leadaxe 2026-07-16 12:45:56 +03:00
parent 831d483366
commit ee7ff1b77f
9 changed files with 502 additions and 12 deletions

View file

@ -215,6 +215,11 @@ func (peer *Peer) SendHandshakeInitiation(isRetry bool) error {
jc := peer.device.junk.count
jmin := peer.device.junk.min
jmax := peer.device.junk.max
if jmax < jmin {
// UAPI validates jmin/jmax only individually; a swapped pair
// would panic rand.Int below with a non-positive bound.
jmin, jmax = jmax, jmin
}
for i := 0; i < jc; i++ {
nBig, _ := rand.Int(rand.Reader, big.NewInt(int64(jmax-jmin+1)))
@ -518,7 +523,9 @@ func (device *Device) InputPacket(destination []byte, packetSlices [][]byte) {
for _, packetSlice := range packetSlices {
totalLength += len(packetSlice)
}
allocLength := MessageEncapsulatingTransportSize + MessageTransportHeaderSize + totalLength + PaddingMultiple + chacha20poly1305.Overhead
// paddings.transport (AWG s4) is prepended in-buffer by
// RoutineSequentialSender; reserve headroom for the shift.
allocLength := MessageEncapsulatingTransportSize + MessageTransportHeaderSize + totalLength + PaddingMultiple + chacha20poly1305.Overhead + device.paddings.transport
if allocLength > MaxMessageSize {
return
}
@ -564,7 +571,9 @@ func (device *Device) InputPackets(packets []*InputPacketRef) []*InputPacketRef
for _, packetSlice := range packetRef.PacketSlices {
totalLength += len(packetSlice)
}
allocLength := MessageEncapsulatingTransportSize + MessageTransportHeaderSize + totalLength + PaddingMultiple + chacha20poly1305.Overhead
// paddings.transport (AWG s4) is prepended in-buffer by
// RoutineSequentialSender; reserve headroom for the shift.
allocLength := MessageEncapsulatingTransportSize + MessageTransportHeaderSize + totalLength + PaddingMultiple + chacha20poly1305.Overhead + device.paddings.transport
if allocLength > MaxMessageSize {
continue
}