From 4200d8a7e0f9da966061cdba51f851596c3d746e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20J=C3=A4mthagen?= Date: Wed, 1 Feb 2017 13:35:28 +0100 Subject: [PATCH] routing: check route length after edges for route is collected This fixes the bug reproduced in #114. The prevHop map in newRoute may include many more edges than what is used to produce the final route, and thus the check prior to building the route could result with incorrect errors being reported. We move this check to after the number of edges to be used for the route is deduced. --- routing/pathfind.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/routing/pathfind.go b/routing/pathfind.go index 23014c8f..a2a7e518 100644 --- a/routing/pathfind.go +++ b/routing/pathfind.go @@ -90,15 +90,6 @@ func computeFee(amt btcutil.Amount, edge *channeldb.ChannelEdge) btcutil.Amount func newRoute(amtToSend btcutil.Amount, source, target vertex, prevHop map[vertex]edgeWithPrev) (*Route, error) { - // As an initial sanity check, the potential route is immediately - // invalidate if it spans more than 20 hops. The current Sphinx (onion - // routing) implementation can only encode up to 20 hops as the entire - // packet is fixed size. If this route is more than 20 hops, then it's - // invalid. - if len(prevHop) > HopLimit { - return nil, ErrMaxHopsExceeded - } - // If the potential route if below the max hop limit, then we'll use // the prevHop map to unravel the path. We end up with a list of edges // in the reverse direction which we'll use to properly calculate the @@ -113,6 +104,14 @@ func newRoute(amtToSend btcutil.Amount, source, target vertex, prev = newVertex(prevHop[prev].prevNode) } + // The route is invalid if it spans more than 20 hops. The current + // Sphinx (onion routing) implementation can only encode up to 20 hops + // as the entire packet is fixed size. If this route is more than 20 hops, + // then it's invalid. + if len(pathEdges) > HopLimit { + return nil, ErrMaxHopsExceeded + } + route := &Route{ Hops: make([]*Hop, len(pathEdges)), }