Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chain service: use safe AddrManager address accessor #230

Closed
wants to merge 1 commit into from

Conversation

chappjc
Copy link
Contributor

@chappjc chappjc commented Oct 18, 2021

This PR requires btcsuite/btcd#1760. The replace would be removed and the btcd require updated if that is merged. The data races between neutrino and btcd are described on that PR.

The *KnownAddress returned by (*AddrManager).GetAddress is not safe for use while other goroutines are using the AddrManager's methods for updating peer states e.g. Connect/Attempt/AddAddresses/SetServices.

The newAddressFunc closure created by NewChainService as the ConnManager's GetNewAddress function was using GetNetAddress concurrent with other peer management goroutines. e.g. a Peer's inHandler that has a callbacks to AddAddresses and SetServices.

This resolves the issue by using the new GetNetAddress method instead of GetAddress. This is safe since the returned wire.NetAddress is immutable, unlike the KnownAddress from GetAddress.

The *KnownAddress returned by (*AddrManager).GetAddress is not safe
for use while other goroutines are using the AddrManager's methods
for updating peer states e.g. Connect/Attempt/AddAddresses/SetServices.

The newAddressFunc closure created by NewChainService as the
ConnManager's GetNewAddress function was using GetNetAddress
concurrent with other peer management goroutines. e.g. a Peer's
inHandler that has a callbacks to AddAddresses and SetServices.

This resolves the issue by using the new GetNetAddress method
instead of GetAddress. This is safe since the returned wire.NetAddress
is immutable, unlike the KnownAddress from GetAddress.
@Roasbeef
Copy link
Member

Thanks for this PR!

Re the underlying bug, is there any concrete unintended interaction that results from it? So like us re-trying the same address over and over again, when we think it's actually a new address?

@chappjc
Copy link
Contributor Author

chappjc commented Oct 18, 2021

is there any concrete unintended interaction that results from it

It's a little hard to tell if there's a specific observable malfunction other than the data race printed, but it's certainly possible that the newAddressFunc was working with invalid addresses and access times given the right circumstances. I think it's mainly about the possibility for memory corruption.

@chappjc
Copy link
Contributor Author

chappjc commented Oct 20, 2021

An update that I've proposed an alternate btcd fix that requires no code changes from neutrino, just a btcd require bump: btcsuite/btcd#1763. dcrd had fixed it that way (add a mutex instead of making copies for external consumers). Either way seems fine to me, although btcsuite/btcd#1763 requires no code changes to neutrino, and it does not touch the API.
I have btcwallet running in SPV mode with this new one and it's doing fine.

@chappjc
Copy link
Contributor Author

chappjc commented Oct 26, 2021

Good news, the alternate btcd fix that requires no consumer code changes was merged btcsuite/btcd#1763
This PR is no longer needed. neutrino may want to update the btcd required, but fortunately that's also something a consumer module can force.

@chappjc chappjc closed this Oct 26, 2021
@chappjc chappjc deleted the safe-addr-manager2 branch October 26, 2021 16:14
@Roasbeef
Copy link
Member

@chappjc ah np, thanks for looking into this in any case! Apologies wasn't able to get to it sooner, focusing on cutting the initial release candidate for lnd 0.14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants