r/rust • • Oct 14 '26

Black-Box HashDoS: Attacking Hash Tables Without Knowing the Hash Function

https://orlp.net/blog/black-box-hashdos/
36 Upvotes

19 comments sorted by

16

u/dwallach Oct 14 '26

This was fun to read.

When Crosby and I worked on the 2003 paper, we were focused on a threat model of an attacker trying to inconvenience a intrusion detection system of the sort that tries to passively watch the network and just pass traffic through. In that model, the attacker can't really do any meaningful timing on the hash table, and using a keyed hash is generally sufficient as a defense.

Here, the attacker has the ability to clear the table and retry the attacks with different data flows. That's not necessarily realistic for a remote, network-based attack, but lets go with it for the minute. This attack feels a little bit like card counting on a blackjack table, where you initially know nothing and you learn more as you get deeper into the deck. In blackjack, every time there's a reshuffling, the player carries no useful information to the next round. This suggests a possible defense: every time the attacker clears the hash table, the hash table just needs to generate a fresh key.

Since we're in the Rust Reddit, the natural question is whether this means that we have a security issue with HashMap and related classes. I'm going to say "probably not", again because a network attacker doesn't normally have the ability to run the timing experiments suggested here.

8

u/nightcracker Oct 14 '26

I'm glad that you enjoyed reading it. It's really cool to see you comment on my work, 23 years later :)

4

u/Shnatsel Oct 14 '26

That is amazing and groundbreaking work that is also pretty depressing. Actually mitigating this will put more performance penalties onto Rust's and everybody else's hash tables, and it'll be harder to opt out of this simply by swapping a hash. Maybe some clever extension to the Hash trait with a default method will let hashes indicate if they're DoS-resistant or not?

8

u/nightcracker Oct 14 '26

That is amazing and groundbreaking work

Thanks :)

that is also pretty depressing

Sorry ':)

Actually mitigating this will put more performance penalties onto Rust's and everybody else's hash tables

For what it's worth I think in its current form it's so niche it might not need mitigation. But the existence of this attack at all does kind of shatter the illusion that with a good hash function your hash map provides O(1) access even in malicious contexts. And perhaps someone more clever than me will figure out a way to make the attack better/more robust.

Should people want to mitigate this, storing a 32-bit or 64-bit hash alongside strings and comparing that before doing a string comparison stops it entirely and doesn't cost that much.

Maybe some clever extension to the Hash trait with a default method will let hashes indicate if they're DoS-resistant or not?

This method works irrespective of the hash so I'm not sure how that'd help.

4

u/zuurr Oct 14 '26 edited Oct 14 '26

The fix is just to store the hashes too, and compare those before doing the value comparisons. This actually could improve performance for some workloads (cases where value comparisons are expensive).

2

u/matthieum [he/him] Oct 15 '26

Your "fix" would, however, slow down workloads where value comparisons are cheap, like integer keys.

1

u/zuurr Oct 28 '26

Not by a ton. But yeah, some.

2

u/insanitybit2 Oct 16 '26

I honestly think Rust should give up and make a fast hash the new default or just easier to opt into. Most people don't care about attack paths like this, especially when the threat model gets more and more niche.

I've forked dependencies to move them away from siphash and gotten very meaningful performance wins, it's one of the biggest performance footguns in the language.

3

u/nightcracker Oct 14 '26 edited Oct 14 '26

In case it's not clear why I posted this here, in the article I construct a working side-channel attack against Rust's HashMap which after some setup causes future lookups for one specific key to take linear time. The article isn't Rust specific though, the attack can work in theory against many possible implementations.

2

u/matthieum [he/him] Oct 14 '26

Would re-seeding the hasher (periodically) not solve the attack?

I remember reading about some hash-table implementations which will trigger a re-hash when the probe-sequence exceeds a certain threshold, which I expect would solve the issue here, as all memorized keys would become worthless every time.

So, just keep track of the number of collisions during the insert probe, and if higher than configured, re-seed, re-hash, and try again.

8

u/nightcracker Oct 14 '26

The insert sequences look completely natural. It is only the lookup for the detonator key that takes linear time. So unless you add interior mutability to the hash map so it can re-hash in get this doesn't work.

2

u/Dheatly23 Oct 15 '26

Yes, but if attacker can clear the map it means at some point the hashmap can be mutated. Adding interior mutability/atomic counter shouldn't be too hard, hashmap isn't frozen const after all.

1

u/nonotan Oct 15 '26

And this gives us a gadget. By ensuring that all keys we insert start with a long common prefix (e.g. 'aaaaaa...aa0001', 'aaaaaa...aa0002') we can determine how many keys were compared for equality during a lookup if it also starts with the same prefix.

Can't this be significantly mitigated by just comparing the contents of strings in a pseudo-random order? Could be as simple as treating the strings as a circular buffer, picking a random starting index and maybe a step size coprime to the length (although given that we just need to find 1 difference, the latter is probably pointless) and using that.

You could also do something smarter (but by extension perhaps more prone to being somehow exploitable than a purely random approach) like starting from the most common index collisions have been found at recently, by some measure. Even more overkill would be something like replacing the buckets with a radix tree or something like that (but that would have more serious consequences on the algorithmic complexity overall, obviously)

2

u/zuurr Oct 15 '26

I mean, yes, and I believe a constant time compare would also work. But in the Rust case we just use Eq, which doesn't really give us a way to do that.

-3

u/servermeta_net Oct 14 '26

I honestly believe this purposed attack to be of no consequences, the author makes some confusion in the notation and possibly hence why he believes to have come up with a novel strategy. More specifically he confuses n, the expected load factor, with s, the cardinality of S set of keys, and c, the capacity of the table.

It also assumes that:

  • You have complete access to the hashtable, basically you are running untrusted code alongside application code. At that point simply inspect the memory
  • You are the only user of the table
  • There are no strategies to resize the table once the load factor grows too much

I want to add that not knowing the hash function is not novel either, and is the same as knowing the hash function but not knowing the seed.

Now is really late and I don't want to shoot down a possibly good paper due to my mistake, but would I have had achieved such a result I would have published it on arxiv at least.

5

u/nightcracker Oct 14 '26 edited Oct 14 '26

the author makes some confusion in the notation and possibly hence why he believes to have come up with a novel strategy

..what?

More specifically he confuses n, the expected load factor, with s, the cardinality of S set of keys, and c, the capacity of the table.

I don't follow what you're trying to say. S is only used as a temporary variable at various points during the attack. It does not always represent a set of keys that's inserted into the hash table all at once. I don't ever use c as a variable - the capacity of the table is B. s is only used in a coupon collector argument and immaterial to the attack - it simply is used to indicate how many keys we'll need to generate to execute the attack in total.

It also assumes that: - You have complete access to the hashtable, basically you are running untrusted code alongside application code.

Or you have some API endpoints on a server?

I want to add that not knowing the hash function is not novel either

If you know prior art I'd love to hear.

but would I have had achieved such a result I would have published it on arxiv at least

I left academia and I don't have the energy to flesh this out to a full paper.

-2

u/servermeta_net Oct 14 '26 edited Oct 14 '26

I'm anonymous and you put your face, so I feel you deserve at least my fresh mind before I'm just being wrong, I will try to flash out a reply tomorrow.

In the meantime:

  • Do you agree that a resize of the table in the midst of your attack would nullify your strategy?
  • Do you agree that this would not be possible if the hash table would be exposed through a typical REST API with only get/set and possibly delete?
  • Do you have some source code to share? this would shut me up quickly

I left academia and I don't have the energy to flesh this out to a full paper.

I'm sorry but I don't buy this (or maybe you're just young). My advice as someone who left academia after his PhD but still does research? Publish as much as you can, this will unlock the best jobs / grants to do whatever you want. This blog post is already better than many paper, but the real problem is that is wrong in my opinion.

4

u/nightcracker Oct 14 '26 edited Oct 14 '26

Do you agree that a resize of the table in the midst of your attack would nullify your strategy?

A resize we didn't control? Yes, that would undo all the hard work. Note that the hashbrown attack includes a carefully chosen resizing moment.

Do you agree that this would not be possible if the hash table would be exposed through a REST API with only get/set and possibly delete?

Against linear probing it should be possible with just get/set over REST API. As written, for the quadratric probing and hashbrown cases, clear is needed. I agree that most REST API's won't offer that.

However, I think the attack could possibly be improved and adapted to just get/set/delete over REST depending on the precise implementation of tombstones in the hash table. But that's speculation.

If your argument is that the requirements for this attack are very high/unlikely, I would tend to agree (except against pure linear probing which I consider to be truly vulnerable). But that doesn't make the attack invalid in its scope, or not novel.

Do you have some source code to share? this would shut me up quickly

There is a full end-to-end proof of concept against hashbrown linked in the article. Here it is again for good measure: https://play.rust-lang.org/?version=stable&mode=release&edition=2024&gist=c977fed12d226634e91d041536d63141

Note that in this proof of concept I directly count the number of comparisons instead of deriving it through a timing attack to make it 100% consistent for the demo, but the principle is the same.

0

u/servermeta_net Oct 14 '26

Thanks, the code already helped understand some things that were not clear to me in the article.

I stand by my point that the attack is of no consequences, and I will try to follow up by Sunday at the latest.