Reject onion per-hop lengths that overflow an `Int` (#3408)
What changed, and why it matters
This fix closes a denial-of-service bug in Eclair's Lightning onion routing. A remote sender could craft a malformed per-hop payload length that, when converted from a large number to a normal integer, wrapped around to a negative or tiny value. That bypassed safety checks and crashed the relayer or the post-restart cleanup logic, leaving affected HTLCs stuck until they expired. The patch now rejects oversized lengths before converting them.
Apply the patch and ensure all nodes are upgraded. Monitor for any related unhandled exceptions in onion peeling paths. Consider additional fuzzing of varint decoders in routing/payment codecs.
Security signals we found
Integer overflow / truncation from Long to Int
Negative length bypasses upper-bound check
Exception escapes Try block causing DoS
Post-restart cleanup abort on unhandled exception
HTLC slot exhaustion / stuck HTLCs until expiry
Remote attacker can compute valid HMAC knowing recipient node id
Evidence from the diff
Sphinx.peekPayloadLength decodes a varint payload length as Long then casts to Int without bounds checking. Malicious varints (e.g., fe 80 00 00 05 or 9-byte ff-prefixed values) can wrap to negative or small positive Ints, evading the upper-bound check in d3a146a08 and causing an out-of-bounds HMAC extraction exception outside the Try. The exception escapes the relayer, preventing upstream HTLC failure, and aborts PostRestartHtlcCleaner on restart. The patch adds a require that payloadLength is non-negative and <= Int.MaxValue - MacLength, converting all cases into a controlled InvalidOnionVersion error.
Changed components
eclair-core/src/main/scala/fr/acinq/eclair/crypto/Sphinx.scalaSphinx.peekPayloadLengthPaymentOnionCodecs.payloadLengthDecoderRelayer / PostRestartHtlcCleanerInspect captured patch +20 / −3
### eclair-core/src/main/scala/fr/acinq/eclair/crypto/Sphinx.scala
@@ -96,7 +96,11 @@ object Sphinx extends Logging {
// Each onion frame contains a variable-length per-hop payload.
// The first bytes contain a varint encoding the length of the payload data (not including the trailing mac).
// Since messages are always smaller than 65535 bytes, this varint will either be 1 or 3 bytes long.
- MacLength + PaymentOnionCodecs.payloadLengthDecoder.decode(payload.bits).require.value.toInt
+ val payloadLength = PaymentOnionCodecs.payloadLengthDecoder.decode(payload.bits).require.value
+ // But the varint may use up to 9 bytes: a malicious sender can choose a value that overflows when converted to an
+ // Int (or even a Long, once the varint size is added) and wraps around to a negative or small positive length.
+ require(0 <= payloadLength && payloadLength <= Int.MaxValue - MacLength, s"invalid per-hop payload length: $payloadLength")
+ MacLength + payloadLength.toInt
}
/**
### eclair-core/src/test/scala/fr/acinq/eclair/crypto/SphinxSpec.scala
@@ -93,6 +93,10 @@ class SphinxSpec extends AnyFunSuite {
// The legacy fixed-size payload is not supported.
assertThrows[IllegalArgumentException](peekPayloadLength(hex"00"))
+ // The varint may encode values that overflow when converted to an Int (or even to a Long).
+ assertThrows[IllegalArgumentException](peekPayloadLength(hex"fe80000005"))
+ assertThrows[IllegalArgumentException](peekPayloadLength(hex"ff0000000100000000"))
+ assertThrows[IllegalArgumentException](peekPayloadLength(hex"ff7fffffffffffffff"))
}
test("is last packet") {
@@ -135,8 +139,17 @@ class SphinxSpec extends AnyFunSuite {
// The sender knows the shared secret, so it can create a packet with a valid hmac that decrypts to an invalid per-hop payload length.
val sharedSecret = computeSharedSecret(publicKeys.head, sessionKey)
val stream = generateStream(generateKey("rho", sharedSecret), 1300)
- for (declaredLength <- Seq(hex"fd0514", hex"fdffff")) {
- val payload = (declaredLength ++ ByteVector.fill(1297)(0)) xor stream
+ val declaredLengths = Seq(
+ hex"fd0514",
+ hex"fdffff",
+ // Lengths that overflow when converted to an Int: the first one wraps around to a negative value, the other two
+ // wrap around to small positive values (the last one even overflows a Long once the varint size is added).
+ hex"fe80000005",
+ hex"ff0000000100000000",
+ hex"ff7fffffffffffffff",
+ )
+ for (declaredLength <- declaredLengths) {
+ val payload = (declaredLength ++ ByteVector.fill(1300 - declaredLength.length)(0)) xor stream
val hmac = mac(generateKey("mu", sharedSecret), payload ++ associatedData.get)
val packet = OnionRoutingPacket(0, sessionKey.publicKey.value, payload, hmac)
val Left(onionErr) = peel(privKeys.head, associatedData, packet)Why this scored 78/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.