only allow GRAFT from outbound peers when we are at or over Dhi

This commit is contained in:
vyzo 2020-05-16 13:05:26 +03:00
parent 39fc35f7cb
commit 7d320ac87a
1 changed files with 20 additions and 0 deletions

View File

@ -587,6 +587,15 @@ func (gs *GossipSubRouter) handleGraft(p peer.ID, ctl *pb.ControlMessage) []*pb.
continue
}
// check the number of mesh peers; if it is at (or over) Dhi, we only accept grafts
// from peers with outbound connections; this is a defensive check to restrict potential
// mesh takeover attacks combined with love bombing
if len(peers) >= gs.Dhi && !gs.isOutboundConnection(p) {
prune = append(prune, topic)
gs.addBackoff(p, topic)
continue
}
log.Debugf("GRAFT: add mesh link from %s in %s", p, topic)
gs.tracer.Graft(p, topic)
peers[p] = struct{}{}
@ -640,6 +649,17 @@ func (gs *GossipSubRouter) handlePrune(p peer.ID, ctl *pb.ControlMessage) {
}
}
func (gs *GossipSubRouter) isOutboundConnection(p peer.ID) bool {
conns := gs.p.host.Network().ConnsToPeer(p)
for _, c := range conns {
if c.Stat().Direction == network.DirOutbound {
return true
}
}
return false
}
func (gs *GossipSubRouter) addBackoff(p peer.ID, topic string) {
gs.doAddBackoff(p, topic, GossipSubPruneBackoff)
}