Skip to content

Fix trivial collisions when hashing structures#9

Merged
orlp merged 1 commit into
orlp:masterfrom
someone12469:struct-fix
Oct 27, 2024
Merged

Fix trivial collisions when hashing structures#9
orlp merged 1 commit into
orlp:masterfrom
someone12469:struct-fix

Conversation

@someone12469

Copy link
Copy Markdown
Contributor

To reproduce:

use std::hash::BuildHasher;
use foldhash::fast::RandomState;
//use ahash::RandomState;
//use std::hash::RandomState;
#[derive(Hash)]
struct Point3 {
    x: u64,
    y: u64,
    z: u64
}
#[derive(Hash)]
struct Point4 {
    x: u64,
    y: u64,
    z: u64,
    w: u64
}
fn main() {
    let st = RandomState::default();
    println!("000: {:?}", st.hash_one(Point3 { x: 0, y: 0, z: 0}));
    println!("001: {:?}", st.hash_one(Point3 { x: 0, y: 0, z: 1}));
    println!("002: {:?}", st.hash_one(Point3 { x: 0, y: 0, z: 2}));
    println!("0000: {:?}", st.hash_one(Point4 { x: 0, y: 0, z: 0, w: 0}));
    println!("0010: {:?}", st.hash_one(Point4 { x: 0, y: 0, z: 1, w: 0}));
    println!("0001: {:?}", st.hash_one(Point4 { x: 0, y: 0, z: 0, w: 1}));
    println!("0011: {:?}", st.hash_one(Point4 { x: 0, y: 0, z: 1, w: 1}));
}

Example output (before fix):

000: 8683679690595636920
001: 8683679690595636920
002: 8683679690595636920
0000: 5859751005930861946
0010: 16699619282587451823
0001: 16699619282587451823
0011: 16699619282587451823

This is caused by write_num resetting sponge_len to 0 when it overflows, causing the next finish or write_num to mistakenly assume it's empty. The bug leads to trivial HashDoS for some structures.

@orlp

orlp commented Oct 27, 2024

Copy link
Copy Markdown
Owner

That is a very silly bug of me, yes. Thanks for the PR.

@orlp orlp merged commit 45a79dc into orlp:master Oct 27, 2024
@Wollac Wollac mentioned this pull request Dec 19, 2024
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.

2 participants