Skip to content

Conversation

@practicalswift
Copy link
Contributor

Add instructions on how to fuzz the P2P layer using Honggfuzz NetDriver.

Honggfuzz NetDriver allows for very easy fuzzing of TCP servers such as Bitcoin Core without having to write any custom fuzzing harness. The bitcoind server process is largely fuzzed without modification.

This makes the fuzzing highly realistic: a bug reachable by the fuzzer is likely also remotely triggerable by an untrusted peer.

@DrahtBot DrahtBot added the Docs label Nov 12, 2020
@Crypt-iQ
Copy link
Contributor

Concept ACK - will be sure to try this out.

Copy link
Contributor

@Crypt-iQ Crypt-iQ left a comment

Choose a reason for hiding this comment

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

Currently testing on my macOS.
Keep getting the following warning line, but seems that coverage still increases:

[2020-11-21T17:11:06-0500][W][91981] netDriver_bindToRndLoopback():143 Could not bind to a random IPv4 Loopback address: Can't assign requested address

@practicalswift
Copy link
Contributor Author

@Crypt-iQ I don't have any Mac to test on, so I'm afraid I cannot help much. Could you add an assertion in PeerManager::ProcessMessage to make sure you're hitting the relevant code paths? :)

Do you have any Linux machine to test under? :)

@Crypt-iQ
Copy link
Contributor

Testing on an Ubuntu machine now.

@Crypt-iQ
Copy link
Contributor

With this method of fuzzing, I think honggfuzz has to guess the handshake by picking multiple correct inputs/messages?

@practicalswift
Copy link
Contributor Author

@Crypt-iQ That is correct (unless we bypass the handshake logic by manually patching it out too)! However, my experience is that Honggfuzz quickly figures that out and achieves meaningful coverage post-handshake. Is that your experience as well? :)

@maflcko
Copy link
Member

maflcko commented Nov 27, 2020

Are crashes reproducible? I guess they might happen to, but generally might require all seeds that have been sent in the past?

@practicalswift
Copy link
Contributor Author

@MarcoFalke

Theoretically both types of issues are certainly possible: reproducible (single input enough to trigger condition) and "non-reproducible" (in this context: single input + previous inputs needed to trigger condition).

Practically:

If this fuzzer hits any issues my guess is that such issues will be reproducible with high probability. That has been the case historically with issues uncovered by src/test/fuzz/process_message or src/test/fuzz/process_messages. They have typically not been dependent on a specific state change being cause by previously proceed input.

Does that answer your question? :)

@maflcko
Copy link
Member

maflcko commented Dec 1, 2020

The reason is that process_messages sends all messages in one input/seed. This one might split them into multiple seeds.

@practicalswift
Copy link
Contributor Author

@MarcoFalke I'm not sure I follow TBH. In the case of HonggFuzz NetDriver each input is handled as if it was a new incoming TCP connection, and each input can contain multiple messages (Bitcoin P2P messages).

The TCP connection is not re-used across inputs if that is what you meant :)

@Crypt-iQ
Copy link
Contributor

Reports a signed integer overflow here:

int64_t nTimeOffset = nTime - GetTime();

net_processing.cpp:2651:37: runtime error: signed integer overflow: -9223372036854775808 - 1612058654 cannot be represented in type 'long'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior net_processing.cpp:2651:37 in

maflcko pushed a commit to bitcoin-core/gui that referenced this pull request Feb 11, 2021
3ddbf22 util: Disallow negative mocktime (MarcoFalke)
f5f2f97 net: Avoid UBSan warning in ProcessMessage(...) (practicalswift)

Pull request description:

  Avoid UBSan warning in `ProcessMessage(...)`.

  Context: bitcoin/bitcoin#20380 (comment) (thanks Crypt-iQ!)

ACKs for top commit:
  MarcoFalke:
    re-ACK 3ddbf22 only change is adding patch written by me
  ajtowns:
    ACK 3ddbf22 -- code review only

Tree-SHA512: e8d7af0457ca86872b75a4e406c0a93aafd841c2962e244e147e748cc7ca118c56be0fdafe53765f4b291410030b2c3cc8f76f733b37a955d34fc885ab6037b9
@practicalswift
Copy link
Contributor Author

practicalswift commented Feb 16, 2021

Given @Crypt-iQ's finding above the value of having Honggfuzz NetDriver in the fuzzing toolbox has been proven empirically I guess :)

Is there anything left to do for this documentation only PR?

@maflcko maflcko merged commit 7f83134 into bitcoin:master Feb 17, 2021
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Feb 17, 2021
…er using Honggfuzz NetDriver

fd0be92 doc: Add instructions on how to fuzz the P2P layer using Honggfuzz NetDriver (practicalswift)

Pull request description:

  Add instructions on how to fuzz the P2P layer using [Honggfuzz NetDriver](http://blog.swiecki.net/2018/01/fuzzing-tcp-servers.html).

  Honggfuzz NetDriver allows for very easy fuzzing of TCP servers such as Bitcoin Core without having to write any custom fuzzing harness. The `bitcoind` server process is largely fuzzed without modification.

  This makes the fuzzing highly realistic: a bug reachable by the fuzzer is likely also remotely triggerable by an untrusted peer.

Top commit has no ACKs.

Tree-SHA512: 9e98cb30f00664c00c8ff9fd224ff9822bff3fd849652172df48dbaeade1dd1a5fc67ae53203f1966a1d4210671b35656009a2d8b84affccf3ddf1fd86124f6e
@Crypt-iQ
Copy link
Contributor

Crypt-iQ commented Feb 18, 2021

I had to comment out the following line before building honggfuzz as otherwise it would stop on my 2015 macbook Arch Linux citing slow execution. Could be the age of my computer though. I bumped the timeout as some of the seeds took >4 seconds, and could not use multiple threads due to a lock on the data directory.

https://github.com/google/honggfuzz/blob/e2acee785aa87a86261c96070cf51b85533257ca/libhfuzz/persistent.c#L78

@practicalswift practicalswift deleted the honggfuzz-p2p-fuzzing branch April 10, 2021 19:43
@Crypt-iQ
Copy link
Contributor

Sorry to gravedig, but I found out why honggfuzz can't get past the version-verack handshake on my machine. Compiling with honggfuzz and -fsanitize=address doesn't instrument strncmp on my build, so it can't guess the message commands. I tried with a very simple external testcase (guess "foo" and panic program) to be sure, and it got stuck.

@practicalswift
Copy link
Contributor Author

@Crypt-iQ Could using a dictionary file containing the commands help overcome that fuzzing hurdle?

@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants