Levelling-up your consensus protocol's testing using Attacknet

ETHCC 7 · 11 Jul 2024 · 22:41

Benjamin Samuels, a security engineer at Trail of Bits, presents Attacknet, an open-source tool for testing consensus protocols against adverse conditions. He explains safety and liveness, then Byzantine faults, node failures, eclipse attacks, network partitions, delayed messages and faulty channels, and argues testnets rarely test these on purpose. Attacknet builds controlled devnets with Kurtosis and applies faults with Chaos Mesh. In his example, clock skew on a node made it send stale attestations, and he says Prysm nodes then ran out of memory replaying state, reproducing a past incident. He notes the flaw was in implementation behavior, not the specification.

Speakers

Topics

From ETHCC 7, hosted by Ethereum France.

Transcript

Hello. Today we're going to be talking about how to level up your uh protocols consensus testing using AttackNet. Before we dive in, uh, a little bit about me, my name's Benjamin Samuels. I'm a security engineer at Trail of Bits. Um Trailer Bits is a security consulting firm. We specialize in research and development. Uh we also maintain some very popular tools: Slither, Echidna, Medusa. Um, and yeah, and uh if you want to follow us on Twitter, there's a QR code here. Uh this will also be at the end, so don't stress too hard if you can't get it now. But before we dive into AttackNet, we have to talk about what do consensus protocols actually do.

Um, and this isn't a formal definition, but it's good enough for us. Uh and basically in a semi-synchronous system, we want to make sure that a set of machines or nodes or validators are gonna agree on a specific state even in the presence of faulty machines. And the faulty machines are the key here, because if you don't have to worry about faulty machines, there's so many easier ways to do it. Like you just do a distributed system, like it's easy. But if we want to be able to handle these faulty machines, we need to have a couple of important properties in the system. And that is safety properties and liveness properties. Safety properties basically revolve around all of the nodes on the network agreeing on the same state, even in the presence of adverse conditions.

And liveness properties basically revolve around the system is going to continue to operate even in the presence of adverse conditions, up to a certain threshold. But both of these only work up to a certain threshold. Like for liveness, usually it's if up to 33% of the network was to like disappear instantly, the network should still produce blocks, it should still agree on the state, it should still have its safety properties held. And what's really important here is the presence of these adverse conditions, because this is really the meat and potatoes of what your consensus protocol has to deal with. And it's very challenging to actually test for these.

So what a lot of protocols tend to do is they uh uh go over it and skip it. But before before we talk about that, let's talk about some of the different kinds of adverse conditions, because like you might have not heard of this term before or be familiar with what uh these consensus protocols actually do or have to handle. Um, first is a Byzantine fault. You've probably heard of this, Byzantine fault tolerance. Uh, it's a you know you hear it all the time, and a lot of people think that this means that there's like actually a malicious attacker on the network doing bad things, and that's not always the case. The formal definition is that it's a process deviation in a node.

That process deviation might be an attacker running their own software. Uh, it might also be a node that's misconfigured, it might also be a node that has like the wrong time set, or you know, it's been hit by a cosmic ray, so it emitted a packet wrong. Uh there's all kinds of different things it could be. Another adverse condition that they have to worry about is fail stop. And we talked about this just a minute ago. It's when nodes go offline. The system has to keep going even when nodes go offline. The next one is uh eclipse attacks and network partitioning.

Uh so Eclipse attacks are basically like a P2P level attack where an attacker joins the network with a bunch of P2P nodes and tries to segregate the validator set, all the honest nodes, and move them and like control their communications to each other to impact the system's either liveness or safety conditions. Um network partitioning is like basically the same thing, but it's kind of like what happens in the wild. So if you were like on IRC 20 years ago and you remember net splits, that's a network partition. It's when you know you split the network into multiple parts, they cannot talk to each other, so they kind of have to just keep going and figure out what to do on their own.

The next one is delayed messages. Um, since this consensus system is running over the internet, duh, um, we have to worry about messages that might take a hundred milliseconds, five hundred milliseconds, five seconds. You have no idea when these messages are going to arrive. And sometimes they don't arrive. And if your protocol can't handle that, then it's going to be a big deal. And the next one is faulty communications channels. So again, this is going over the internet. So you might have packet corruption, you might have um uh packets that are duplicated messages that are duplicated messages that have all kinds of issues.

Um, but basically these are your adverse conditions, and when you're designing a consensus protocol like Tendermint or like Gasper or any of that, you have to think about how are we actually going to test to make sure that the implementation of the protocol is going to be able to handle this because it's completely different designing the specification of the protocol and being like, oh yeah, on paper, we can handle this, this, and this because of these equations, and then having the implementation actually be able to do the same thing. The implementation is often a lot trickier than the actual specification, and sometimes securing it is a lot harder. So let's talk about how most protocols, like consensus protocols, test these conditions.

It's actually a very similar test method for all of them. They very heavily rely on test nets. And there's a couple of like little uh exclusions of examples of where they don't use test nets. They might set up like an EDE test harness or something like that. But for the most part, everyone relies on these test nets. So, like the natural next question is how are these test nets actually verifying these different adverse conditions? What's actually going on to make sure that the network can deal with these problems? And for Byzantine faults, uh, you're hoping that someone on your network is either gonna run a Byzantine node or misconfigured node.

Is that gonna happen? Maybe. Um you could have someone from your foundation run a Byzantine node on the network, but then you know you still have to set that up. For fail stop attacks, uh, you're hoping that you know, at some point during your test net, uh, a third of the network is going to go offline. Um the normal churn on a network is not very much. Like maybe something bad will happen, you'll have 5% of the network go offline because of a bug. But as I said earlier, the liveness expectations of a blockchain network is that 33% of the nodes can go offline and it'll keep operating. Now, if your foundation like controls all the nodes on the test net, maybe you could orchestrate this and be like, all right, we're gonna we're gonna shut down 33% of the network at the same time.

And that works, but it's still something that's a lot of work to set up, especially if you have like a decentralized test net. Uh the next one is Eclipse attacks and partitioning. This one's even more challenging to test. Uh, and test nets uh do not get very good coverage here. You're basically hoping that there is going to be something like a BGP hijacking. Um that's where you know the internet backbone providers all are very confused and they don't know where to send their traffic. And this isn't all this is also not necessarily representative of what it would look like if an attacker was infiltrating your P2P network. Whoops, I skipped ahead.

But uh for delayed messages, uh, you're hoping that someone's running a validator in Australia, and while that's great, uh you'll get your 500 millisecond test, but your protocol has to endure more than a 500 millisecond delay. There's gonna be an upper bound to that delay, it has to endure. And if like let's say you want to test this on your test net and your your foundation is like, yeah, we're gonna do this. Um, well, you have to figure out a way to delay those packets, and now you have to start writing firewall rules. And if you want to do the same thing, like maybe you want 20% of the nodes on your network to all start having delayed messages, and now things get really, really tricky to set up. Whoops.

Last one faulty communications channel. This is like the same kind of vibe. You're hoping that something bad happens. Maybe you could set up something bad manually, but it's gonna be really tricky. Uh the theme behind all of these is a lot of protocols don't do any of these explicitly. They just let the test net do it, and it gets some coverage, but it's not the best. Um, I know the Ethereum Foundation is very, very careful about setting up test nets to like try to test some of these explicitly, and they're really good at it. Um, but like there's a whole bunch of other networks out there. They're just like testnet, did it work for a couple months?

Did it shut down? Alright, let's go to production. And that's not good. So that's kind of where the idea for Tacnet came came from. We wanted this to see what happens if we could build like some kind of DevNet clean room where we could control the environment, manually create test cases, and do something very repeatable. And so how it would work is we're gonna use a tool called Kurtosis to generate DevNets. If you haven't heard of Kurtosis, it's very similar to Helm and Chef and a bunch of other deployment tools. It can do all of the Genesis work, like generating your validator keys, uh figuring out where your Genesis params are, generating the configurations for all of the nodes, and it does that all on its own.

Very good. Next thing we're gonna do is we're gonna deploy a bunch of nodes to Kubernetes. Kubernetes, I'd normally not recommend it because it has problems. But in this case, it actually is a very good application of Kubernetes, because Kubernetes has fantastic scaling and control benefits. If you want to run a devnet with a hundred thousand a million nodes, you can totally do it, and AWS would be happy to provision it for you. You also get these control benefits because you can control the networking plane, you can control how the nodes talk to each other, uh, you can do a lot of really powerful stuff. The next thing is you're going to involve in inject faults with chaos mesh.

So chaos mesh is a chaos engineering tool. And if you're not familiar with chaos engineering, it's this idea that was popularized by Netflix a couple of years ago. Uh they made this tool called Chaos Monkey, and what it would do is it'd run around their back end and it randomly uh shut down servers, just shut them down. And if it caused an outage, it'd create an incident, and the engineers were responsible for finding out how to re-engineer their back end. So if Chaos Monkey was to destroy that server again, would it be able to recover? And so Chaos Mesh is a tool kind of built, you know, to respect that.

It does a lot more than just shutting down nodes. It can do all kinds of things we'll talk about in a minute. And we use it to inject this chaos into the Kubernetes cluster. And then we figure out if the network failed. Like for some faults, like you just want to see, oh, we're gonna knock off 33% of the network, we're gonna inject this latency, and we just want to see if the network is gonna keep operating afterwards. Or maybe you're gonna actually exceed some of those thresholds from earlier, kill 50% of the network, and see if we bring those nodes back online, are they actually going to recover? So this is uh the same kind of like chart as earlier.

Uh we have some of our adverse conditions here, and we're going to talk about how we can test the implementation using AttackNet. For Byzantine faults, there's a lot of different ways you could do this, but the most interesting one that we've gotten the best results with is skewing the nodes clock. And so what that actually means is like right now it's like 3.47 p.m. If you skew a single validator's clock backwards or forwards by like 10 minutes, what will happen on the network? And we've seen that that's produced some very interesting behavior. Uh for fail stop, it's very simple.

You just kill the pods in Kubernetes, you see how things respond, and bing bada boom. Uh for Eclipse attacks, uh, Chaos Mesh has a lot of control over the network because everything is running in Kubernetes. It can inject IP tables rules, it can control which nodes talk to which other nodes. So if we want to set up like a very specific eclipse attack or network partition, we can totally do that. For delayed messages and faulty communications channels, um, chaos mesh has a bunch of additional tools like uh there's a latency injector, so you can have an arbitrary amount of latency injected into the system, um, or even like just between two pods or something. Uh you could constrain the bandwidth and make sure that it actually meets the spec that you're publishing, and that you know uh your node isn't gonna mysteriously start consuming like twice the amount of bandwidth you recommend.

And then for faulty communications channels, you have things like the packet dropper, packet corruptor, and duplication. So we the original idea for this tool came around during the Ethereum finality incident. Right afterwards, we were wondering would it be popular, would it be possible to detect this with a tool beforehand? And if you're not familiar with the Ethereum finality incident, essentially what happened is there was a node on the network, it started emitting stale attestations, we don't know why. Uh these stale attestations took a lot of resources to validate. They were they were stale.

They had to the state had to be rebuilt to verify the attestation was valid. It was very rough. Uh and the memory usage on a bunch of validators went way up to rebuild the state and validate those attestations. And uh so nodes went offline, and the network lost finality for a couple epochs. And I think the most interesting thing about this is that uh this issue was not detected during any test nets. Uh, there's a lot of reasons for this, but uh it kind of reinforces this idea that uh test nets, while they are you know effective at finding some classes of bugs, uh, if you aren't explicitly like adding testing criteria to the test net, like you're not really gonna get the coverage that you want. So then we went into how could we possibly detect this with the tagnet?

Could we reproduce it? Uh or if this was just a dumb idea. And so what we ended up landing on, we tried a bunch of different faults, but the one that was the most interesting was using a clock skew to induce a Byzantine fault. And this is the this configuration is what we landed on. So we were targeting a lighthouse geth validator on this network. Uh we skewed the clock on this node back by 10 minutes, uh, and we did this for 20 minutes. And here's what this like actually looked like. So imagine it's 9.10 a.m., that's the true time.

You have our nodes on the network, they're all valid, they're all validating, they're emitting attestations, they're all very happy. Uh, and that's the time that each node thinks it is. And then at 9.11, uh, this fault is injected in node one. So uh node one's clock now skews backwards by 10 minutes, and it thinks it's 9.01 a.m. Um, there is all kinds of different behaviors we observed for different kinds of nodes when this happened, but for this specific combination, what would happen is the node would think that it was not synced because there's blocks in its database that were newer than what the time would indicate. And uh it would just kind of like stop emitting attestations and start going into like this kind of syncing mode.

But the rest of the network is chilling, it's still emitting attestations, everything's good. Now, when this gets interesting, is when 10 minutes later, the clock on node one has now caught up to when the fault was injected. It's now 9-11 a.m. as far as it's concerned, and so as far as it's concerned, it is now synced up to the head of the network. It's time to start emitting attestations again. But when it starts emitting these attestations, they're stale. They're for this snapshot in time that was 10 minutes ago. And the other nodes in the network, they received the these attestations. And in order to verify these attestations, they had to replay the state of the blockchain back to 10 minutes ago to verify them.

And so like this is like in theory what would happen, but we actually tested this and have like actual results. And this is what it looks like in Grafana. So this graph on the top left is how the head of the chain for each of these nodes is like progressing over time. You can see at 9.11, it stops keeping up with the chain. It just holds right there at like log 480 or something. And then 10 minutes later, as far as it's concerned, it's caught up and it starts trying to sync. And I'm not sorry, not sync, starts to emit attestations again.

And when it emits these attestations, they're stale. They're for blocks that were 10 minutes ago. And the Prism nodes start trying to validate them, and they do this thing called replaying the state, so they can actually figure out is this valid? Is this attestation is this attested? Is this attestation valid or is it not? And it's really interesting. So Prism, when when I originally ran this, what would happen, I thought something was wrong on my side because I'd come back 10 minutes later and all the Prism nodes were dead. And I then did this again with way, way, way more memory.

And you can see here, you can see when they did this replay blocks, uh, there's a big spike in memory consumption. And under like any normal conditions, like the recommended like node memory amount, this would cause the node to crash. So in effect, we were able to reproduce this incident using this Byzantine clock skew. So this was actually a really, really good result because I wasn't confident it was gonna work. And like this is so experimental. And this is such a good result.

And this is like reinforcing that we really should be explicitly testing for some of these cases, even on like big networks that know what they're doing, like Ethereum. I can't even, I'm I can't wait to run this on like you know some other networks. Um another thing is uh this was not a bug in the spec. There's nothing wrong with the specification here. This was a thing in the implementation, not even a bug in the implementation, it is just like a behavior in the implementation that causes behavior. And so, like if you like had someone audit your spec, they would have never found this. Yeah, and so our conclusion here is test nets are great.

I don't I don't want the thesis here to be test nets suck because they don't suck. They're fantastic for end users, they're great for testing things on not mainnet. But test nets are not great at verifying these consensus properties. Uh maybe, maybe for certain very specific kinds of tests, they're good. But for the stuff that we've talked about, I think doing it in a controlled environment will yield much better results. And uh if you're like thinking of creating a consensus protocol, God forbid, uh, you should definitely be explicitly testing for these. Do not just trust your your spec says it's okay, so that's okay.

Your unit tests say it's okay, so that's okay. You really want to be testing these live with your implementation. And uh that kind of wraps it up. Uh attack net is open source under AGPLV3. You can go to it right there. Um Thanks again to the Ethereum Foundation for co-funding this project and working with me. Uh, we're also hiring security engineers. If you think any of this is interesting, or you want to work on it on like work on something like this or work on this, uh, definitely reach out. And uh yeah, if you want to talk to me or like you're interested in running this or you have any questions, feel free to reach out on Twitter or Telegram right there. And there's the the Twitter thing again.

How's the time look? Uh that's it. So that's it? Okay. No time. Well, if you have any questions, come up to me.

Related talks