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

fuzz: call lookup functions before calling Ban #27935

Merged
merged 2 commits into from Nov 13, 2023

Conversation

brunoerg
Copy link
Contributor

@brunoerg brunoerg commented Jun 22, 2023

Fixes #27924

To not have any discrepancy, it's required to call lookup functions before calling Ban. If we don't do it, the assertion assert(banmap == banmap_read); may fail because BanMapFromJson will call LookupSubNet and cause the discrepancy between the banned and the loaded one. It happens especially in MacOS (#27924).

Also, calling lookup functions before banning is what RPC setban does.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 22, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK maflcko, dergoegge
Stale ACK jonatack

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

@DrahtBot DrahtBot added the Tests label Jun 22, 2023
@brunoerg brunoerg changed the title fuzz: call LookupSubNet before calling Ban fuzz: call LookupSubNet before calling Ban with a subnet Jun 22, 2023
@brunoerg brunoerg force-pushed the 2023-06-fuzz-banman-ban branch 2 times, most recently from c6a72ad to ecffb66 Compare June 22, 2023 19:47
@maflcko
Copy link
Member

maflcko commented Jun 29, 2023

Could rebase on master to remove export FUZZ_TESTS_CONFIG="--exclude banman" # https://github.com/bitcoin/bitcoin/issues/27924?

@brunoerg
Copy link
Contributor Author

Could rebase on master to remove export FUZZ_TESTS_CONFIG="--exclude banman" # #27924?

Sure.

@brunoerg
Copy link
Contributor Author

Force-pushed to remove "--exclude banman" in Mac

@brunoerg
Copy link
Contributor Author

Rebased.

@dergoegge
Copy link
Member

This still crashes for me on the following input /r8BAAAC/9LcAF8gMiWADQUA//////////////8AN1wNXMnJyQ3///8Afv///wABAAAAgAAAAADPvg==

@brunoerg
Copy link
Contributor Author

I'm investigating it.

@brunoerg brunoerg marked this pull request as draft August 17, 2023 12:42
@brunoerg brunoerg marked this pull request as ready for review August 17, 2023 14:07
@brunoerg brunoerg changed the title fuzz: call LookupSubNet before calling Ban with a subnet fuzz: call lookup functions before calling Ban Aug 17, 2023
@brunoerg
Copy link
Contributor Author

brunoerg commented Aug 17, 2023

Thanks for reviewing, @dergoegge. I noted that we also need to call LookupHost when banning with CNetAddr. Force-pushed doing it and updated the PR description and title.

Also, this way we're closer to the reality (following what RPC does to set it).

Copy link
Contributor

@jonatack jonatack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK f4bd8f2

Happy to re-ack if you take the suggestion.

src/test/fuzz/banman.cpp Outdated Show resolved Hide resolved
src/test/fuzz/banman.cpp Outdated Show resolved Hide resolved
@vasild
Copy link
Contributor

vasild commented Oct 25, 2023

I guess there are two possible approaches:

  1. Shorter and less invasive/risky - change the fuzz test to not ban invalid subnets. Calling LookupSubNet() should not be necessary and I guess IsValid() should suffice? Is there a case where a subnet has IsValid() == true but LookupSubNet() fails on it? Also, IPv6 subnets that start with fc while NET_CJDNS is reachable are "invalid".

  2. Change BanMan::Ban() to not add such invalid networks to its internal map and change it to return bool, false meaning "I did not ban that" and true meaning "A ban was added".

Maybe do 1. for 26.0 and 2. for 27.0?

@maflcko
Copy link
Member

maflcko commented Oct 30, 2023

Looks like this can't be merged/backported as-is either way, due to the silent conflict caused by the rafactor (7be62df)?

@brunoerg
Copy link
Contributor Author

Looks like this can't be merged/backported as-is either way, due to the silent conflict caused by the rafactor (7be62df)?

Gonna check it

@vasild
Copy link
Contributor

vasild commented Oct 31, 2023

One more consideration wrt to changing Ban() to refuse invalid stuff (2. in above #27935 (comment)) - it will not match later anyway due to:

bitcoin/src/netaddress.cpp

Lines 1005 to 1008 in 4458ae8

bool CSubNet::Match(const CNetAddr &addr) const
{
if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
return false;

@brunoerg
Copy link
Contributor Author

Rebased and changed the approach in fuzz to call Ban only with valid net address/subnet.

@maflcko
Copy link
Member

maflcko commented Nov 8, 2023

Are you still working on this?

@brunoerg
Copy link
Contributor Author

brunoerg commented Nov 8, 2023

Are you still working on this?

Yes, will push an update soon

@brunoerg
Copy link
Contributor Author

brunoerg commented Nov 8, 2023

Force-pushed:

  • Added a check to control whether we called Ban with an invalid subnet/netaddr. If so, we don't compare the banmaps.
  • I did some experiments and I decided to keep the lookup functions. Without it, I still get discrepances even avoiding the comparison when we call Ban with an invalid subnet/netaddr.

@maflcko
Copy link
Member

maflcko commented Nov 9, 2023

I did some experiments and I decided to keep the lookup functions. Without it, I still get discrepances even avoiding the comparison when we call Ban with an invalid subnet/netaddr.

Would be good to explain this a bit more, to make sure this is not a bug.

@brunoerg
Copy link
Contributor Author

brunoerg commented Nov 9, 2023

Would be good to explain this a bit more, to make sure this is not a bug.

I think fuzz can generate subnets with a zone identifier, like: "676:c962:7962:b787:b392:fed8:7058:c500%2038004089/121", which is a valid one. However, the lookup call might change the zone identifier. In my machine (macOS), "676:c962:7962:b787:b392:fed8:7058:c500%2038004089/121" becomes "676:c962:7962:b787:b392:fed8:7058:c500%31097/121" after lookup and it makes the assertion fails.

@maflcko
Copy link
Member

maflcko commented Nov 9, 2023

Ah ok. And about the efficiency, is it possible to keep the fuzz input format unchanged instead of using a string representation (Maybe via a string-lookup roundtrip)? I wonder if that'd be more efficient.

@brunoerg
Copy link
Contributor Author

brunoerg commented Nov 9, 2023

Ah ok. And about the efficiency, is it possible to keep the fuzz input format unchanged instead of using a string representation (Maybe via a string-lookup roundtrip)? I wonder if that'd be more efficient.

Yes, force-pushed addressing it. Also, I think this way our seeds remain great.

@maflcko
Copy link
Member

maflcko commented Nov 9, 2023

lgtm ACK fca0a89

I did not check the performance difference against 7e64430

Copy link
Member

@dergoegge dergoegge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK fca0a89

@fanquake fanquake merged commit e862bce into bitcoin:master Nov 13, 2023
16 checks passed
@vasild
Copy link
Contributor

vasild commented Nov 13, 2023

ACK fca0a89

luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Mar 8, 2024
Also, compare banmaps only if there are no invalid
entries.

Github-Pull: bitcoin#27935
Rebased-From: f9b2863
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Mar 8, 2024
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Mar 13, 2024
Also, compare banmaps only if there are no invalid
entries.

Github-Pull: bitcoin#27935
Rebased-From: f9b2863
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fuzz: banman, Assertion `banmap == banmap_read' failed
7 participants