Reject onion per-hop lengths exceeding packet (#3395)
What changed, and why it matters
This commit fixes a bug in how Eclair processes encrypted routing instructions (onion packets) for Lightning payments. A malicious sender could craft a packet that claimed its inner payload was longer than the packet itself. The old code either produced garbage or crashed outside of the expected error-handling path. That crash could escape the payment relayer, meaning the node would not properly fail the affected payment, and it could also break startup recovery code after a restart. The fix adds a length check and returns a standard invalid-onion error instead of crashing.
Treat this as a security fix and include it in the next maintenance release. Nodes should upgrade promptly because the bug is remotely triggerable by any peer that can send an HTLC with a crafted onion packet, and it can impair both live relaying and restart recovery. Review other variable-length parsing paths in Sphinx for similar missing upper-bound checks.
Security signals we found
Unchecked length field from attacker-controlled onion packet used for slicing
Exception thrown outside Try block, bypassing expected error handling
Relayer crash could leave HTLC un-failed
PostRestartHtlcCleaner initialization failure on next restart
Fix maps malformed input to standard InvalidOnionVersion failure
Evidence from the diff
In Sphinx.peel, after decrypting an onion packet, the code calls peekPayloadLength to read the variable-length per-hop payload size. Previously it only checked whether peekPayloadLength succeeded, not whether the declared length fit within the decrypted packet payload. If perHopPayloadLength > packet.payload.length, the subsequent slice for the HMAC could throw outside the Try, bypassing normal error handling. The patch adds a guard (perHopPayloadLength <= packet.payload.length) and maps both failure and oversize cases to Left(InvalidOnionVersion(…)). A test is added that builds a packet with a valid HMAC but a declared per-hop length of 0x0514 (1300) and 0xffff (65535) bytes, both larger than the 1300-byte payload, and asserts the expected failure.
Changed components
eclair-core/src/main/scala/fr/acinq/eclair/crypto/Sphinx.scalaSphinx.peel onion decryption logicRelayer HTLC processingPostRestartHtlcCleaner startup recoveryInspect captured patch +16 / −2
### eclair-core/src/main/scala/fr/acinq/eclair/crypto/Sphinx.scala
@@ -172,13 +172,14 @@ object Sphinx extends Logging {
val stream = generateStream(rho, 2 * packet.payload.length.toInt)
val bin = (packet.payload ++ ByteVector.fill(packet.payload.length)(0)) xor stream
Try(peekPayloadLength(bin)) match {
- case Success(perHopPayloadLength) =>
+ // The per-hop payload (including its hmac) must fit in the packet, otherwise we may throw when extracting the hmac.
+ case Success(perHopPayloadLength) if perHopPayloadLength <= packet.payload.length =>
val perHopPayload = bin.take(perHopPayloadLength - MacLength)
val hmac = ByteVector32(bin.slice(perHopPayloadLength - MacLength, perHopPayloadLength))
val nextOnionPayload = bin.drop(perHopPayloadLength).take(packet.payload.length)
val nextPubKey = blind(packetEphKey, computeBlindingFactor(packetEphKey, sharedSecret))
Right(DecryptedPacket(perHopPayload, OnionRoutingPacket(Version, nextPubKey.value, nextOnionPayload, hmac), sharedSecret))
- case Failure(_) =>
+ case _ =>
Left(InvalidOnionVersion(hash(packet)))
}
} else {
### eclair-core/src/test/scala/fr/acinq/eclair/crypto/SphinxSpec.scala
@@ -131,6 +131,19 @@ class SphinxSpec extends AnyFunSuite {
}
}
+ test("bad onion: per-hop payload length exceeds packet length") {
+ // 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 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)
+ assert(onionErr == InvalidOnionVersion(hash(packet)))
+ }
+ }
+
test("create payment packet (reference test vector)") {
val Success(PacketAndSecrets(onion, sharedSecrets)) = create(sessionKey, 1300, publicKeys, referencePaymentPayloads, associatedData)
assert(serializePaymentOnion(onion) == hex"0002eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619f7f3416a5aa36dc7eeb3ec6d421e9615471ab870a33ac07fa5d5a51df0a8823aabe3fea3f90d387529d4f72837f9e687230371ccd8d263072206dbed0234f6505e21e282abd8c0e4f5b9ff8042800bbab065036eadd0149b37f27dde664725a49866e052e809d2b0198ab9610faa656bbf4ec516763a59f8f42c171b179166ba38958d4f51b39b3e98706e2d14a2dafd6a5df808093abfca5aeaaca16eded5db7d21fb0294dd1a163edf0fb445d5c8d7d688d6dd9c541762bf5a5123bf9939d957fe648416e88f1b0928bfa034982b22548e1a4d922690eecf546275afb233acf4323974680779f1a964cfe687456035cc0fba8a5428430b390f0057b6d1fe9a8875bfa89693eeb838ce59f09d207a503ee6f6299c92d6361bc335fcbf9b5cd44747aadce2ce6069cfdc3d671daef9f8ae590cf93d957c9e873e9a1bc62d9640dc8fc39c14902d49a1c80239b6c5b7fd91d05878cbf5ffc7db2569f47c43d6c0d27c438abff276e87364deb8858a37e5a62c446af95d8b786eaf0b5fcf78d98b41496794f8dcaac4eef34b2acfb94c7e8c32a9e9866a8fa0b6f2a06f00a1ccde569f97eec05c803ba7500acc96691d8898d73d8e6a47b8f43c3d5de74458d20eda61474c426359677001fbd75a74d7d5db6cb4feb83122f133206203e4e2d293f838bf8c8b3a29acb321315100b87e80e0edb272ee80fda944e3fb6084ed4d7f7c7d21c69d9da43d31a90b70693f9b0cc3eac74c11ab8ff655905688916cfa4ef0bd04135f2e50b7c689a21d04e8e981e74c6058188b9b1f9dfc3eec6838e9ffbcf22ce738d8a177c19318dffef090cee67e12de1a3e2a39f61247547ba5257489cbc11d7d91ed34617fcc42f7a9da2e3cf31a94a210a1018143173913c38f60e62b24bf0d7518f38b5bab3e6a1f8aeb35e31d6442c8abb5178efc892d2e787d79c6ad9e2fc271792983fa9955ac4d1d84a36c024071bc6e431b625519d556af38185601f70e29035ea6a09c8b676c9d88cf7e05e0f17098b584c4168735940263f940033a220f40be4c85344128b14beb9e75696db37014107801a59b13e89cd9d2258c169d523be6d31552c44c82ff4bb18ec9f099f3bf0e5b1bb2ba9a87d7e26f98d294927b600b5529c47e04d98956677cbcee8fa2b60f49776d8b8c367465b7c626da53700684fb6c918ead0eab8360e4f60edd25b4f43816a75ecf70f909301825b512469f8389d79402311d8aecb7b3ef8599e79485a4388d87744d899f7c47ee644361e17040a7958c8911be6f463ab6a9b2afacd688ec55ef517b38f1339efc54487232798bb25522ff4572ff68567fe830f92f7b8113efce3e98c3fffbaedce4fd8b50e41da97c0c08e423a72689cc68e68f752a5e3a9003e64e35c957ca2e1c48bb6f64b05f56b70b575ad2f278d57850a7ad568c24a4d32a3d74b29f03dc125488bc7c637da582357f40b0a52d16b3b40bb2c2315d03360bc24209e20972c200566bcf3bbe5c5b0aedd83132a8a4d5b4242ba370b6d67d9b67eb01052d132c7866b9cb502e44796d9d356e4e3cb47cc527322cd24976fe7c9257a2864151a38e568ef7a79f10d6ef27cc04ce382347a2488b1f404fdbf407fe1ca1c9d0d5649e34800e25e18951c98cae9f43555eef65fee1ea8f15828807366c3b612cd5753bf9fb8fced08855f742cddd6f765f74254f03186683d646e6f09ac2805586c7cf11998357cafc5df3f285329366f475130c928b2dceba4aa383758e7a9d20705c4bb9db619e2992f608a1ba65db254bb389468741d0502e2588aeb54390ac600c19af5c8e61383fc1bebe0029e4474051e4ef908828db9cca13277ef65db3fd47ccc2179126aaefb627719f421e20")Why this scored 74/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.