From 37619126805656c2e2eab06a892e9ea0b2703912 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Mon, 26 Feb 2024 13:10:22 -0800 Subject: [PATCH] htlcswitch: avoid leaking peer interface from link Here we notice that the only use of the Peer call on the link is to find out what the peer's pubkey is. To avoid leaking handles to IO actions outside the interface we reduce the surface area to just return the peer's public key. --- htlcswitch/interfaces.go | 7 +++---- htlcswitch/link.go | 4 ++-- htlcswitch/mock.go | 20 ++++++++++++++++---- htlcswitch/switch.go | 10 +++++----- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/htlcswitch/interfaces.go b/htlcswitch/interfaces.go index 75866beba..ac3e10bab 100644 --- a/htlcswitch/interfaces.go +++ b/htlcswitch/interfaces.go @@ -7,7 +7,6 @@ import ( "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb/models" "github.com/lightningnetwork/lnd/invoices" - "github.com/lightningnetwork/lnd/lnpeer" "github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lnwallet/chainfee" "github.com/lightningnetwork/lnd/lnwire" @@ -262,9 +261,9 @@ type ChannelLink interface { // total sent/received milli-satoshis. Stats() (uint64, lnwire.MilliSatoshi, lnwire.MilliSatoshi) - // Peer returns the representation of remote peer with which we have - // the channel link opened. - Peer() lnpeer.Peer + // Peer returns the serialized public key of remote peer with which we + // have the channel link opened. + PeerPubKey() [33]byte // AttachMailBox delivers an active MailBox to the link. The MailBox may // have buffered messages. diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 944ac0591..0b1a3b160 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -2552,8 +2552,8 @@ func (l *channelLink) updateCommitTx() error { // channel link opened. // // NOTE: Part of the ChannelLink interface. -func (l *channelLink) Peer() lnpeer.Peer { - return l.cfg.Peer +func (l *channelLink) PeerPubKey() [33]byte { + return l.cfg.Peer.PubKey() } // ChannelPoint returns the channel outpoint for the channel link. diff --git a/htlcswitch/mock.go b/htlcswitch/mock.go index ab6fbe76a..e995b3ed7 100644 --- a/htlcswitch/mock.go +++ b/htlcswitch/mock.go @@ -891,10 +891,22 @@ func (f *mockChannelLink) Start() error { return nil } -func (f *mockChannelLink) ChanID() lnwire.ChannelID { return f.chanID } -func (f *mockChannelLink) ShortChanID() lnwire.ShortChannelID { return f.shortChanID } -func (f *mockChannelLink) Bandwidth() lnwire.MilliSatoshi { return 99999999 } -func (f *mockChannelLink) Peer() lnpeer.Peer { return f.peer } +func (f *mockChannelLink) ChanID() lnwire.ChannelID { + return f.chanID +} + +func (f *mockChannelLink) ShortChanID() lnwire.ShortChannelID { + return f.shortChanID +} + +func (f *mockChannelLink) Bandwidth() lnwire.MilliSatoshi { + return 99999999 +} + +func (f *mockChannelLink) PeerPubKey() [33]byte { + return f.peer.PubKey() +} + func (f *mockChannelLink) ChannelPoint() *wire.OutPoint { return &wire.OutPoint{} } func (f *mockChannelLink) Stop() {} func (f *mockChannelLink) EligibleToForward() bool { return f.eligible } diff --git a/htlcswitch/switch.go b/htlcswitch/switch.go index fa98cca11..0e2101027 100644 --- a/htlcswitch/switch.go +++ b/htlcswitch/switch.go @@ -1147,7 +1147,7 @@ func (s *Switch) handlePacketForward(packet *htlcPacket) error { return s.failAddPacket(packet, linkError) } - targetPeerKey := targetLink.Peer().PubKey() + targetPeerKey := targetLink.PeerPubKey() interfaceLinks, _ := s.getLinks(targetPeerKey) s.indexMtx.RUnlock() @@ -1810,9 +1810,9 @@ out: } s.indexMtx.RUnlock() - peerPub := link.Peer().PubKey() + peerPub := link.PeerPubKey() log.Debugf("Requesting local channel close: peer=%v, "+ - "chan_id=%x", link.Peer(), chanID[:]) + "chan_id=%x", link.PeerPubKey(), chanID[:]) go s.cfg.LocalChannelClose(peerPub[:], req) @@ -2335,7 +2335,7 @@ func (s *Switch) addLiveLink(link ChannelLink) { // Next we'll add the link to the interface index so we can // quickly look up all the channels for a particular node. - peerPub := link.Peer().PubKey() + peerPub := link.PeerPubKey() if _, ok := s.interfaceIndex[peerPub]; !ok { s.interfaceIndex[peerPub] = make(map[lnwire.ChannelID]ChannelLink) } @@ -2610,7 +2610,7 @@ func (s *Switch) removeLink(chanID lnwire.ChannelID) ChannelLink { // If the link has been added to the peer index, then we'll move to // delete the entry within the index. - peerPub := link.Peer().PubKey() + peerPub := link.PeerPubKey() if peerIndex, ok := s.interfaceIndex[peerPub]; ok { delete(peerIndex, link.ChanID())