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

protocols/kad: Fix right shift overflow panic in record_received #1492

Merged
merged 7 commits into from
Mar 18, 2020
16 changes: 10 additions & 6 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,8 +946,6 @@ where
return
}

let now = Instant::now();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is preferable to leave this here as it can be reused multiple times (cf. #1496). And since Instant::now() is a side-effecting function for obtaining some notion of system time, usually both testability and performance is improved by using it mostly in top-level functions and passing the instant down to others where needed (see also the other comments).


// Calculate the expiration exponentially inversely proportional to the
// number of nodes between the local node and the closest node to the key
// (beyond the replication factor). This ensures avoiding over-caching
Expand All @@ -956,9 +954,7 @@ where
let num_between = self.kbuckets.count_nodes_between(&target);
let k = self.queries.config().replication_factor.get();
let num_beyond_k = (usize::max(k, num_between) - k) as u32;
let expiration = self.record_ttl.map(|ttl|
now + Duration::from_secs(ttl.as_secs() >> num_beyond_k)
);
let expiration = exp_decr_expiration(self.record_ttl, num_beyond_k);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let expiration = exp_decr_expiration(self.record_ttl, num_beyond_k);
let expiration = self.record_ttl.map(|ttl| now + exp_decrease(ttl, num_beyond_k));

// The smaller TTL prevails. Only if neither TTL is set is the record
// stored "forever".
record.expires = record.expires.or(expiration).min(expiration);
Expand Down Expand Up @@ -1030,6 +1026,15 @@ where
}
}

/// Calculate exponentially decreasing expiration from a default time-to-live by a factor.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Calculate exponentially decreasing expiration from a default time-to-live by a factor.
/// Exponentially decrease the given duration (base 2).

fn exp_decr_expiration(default_ttl: Option<Duration>, factor: u32) -> Option<Instant> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
fn exp_decr_expiration(default_ttl: Option<Duration>, factor: u32) -> Option<Instant> {
fn exp_decrease(ttl: Duration, exp: u32) -> Instant {

default_ttl.map(|ttl| Instant::now() + Duration::from_secs(
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
default_ttl.map(|ttl| Instant::now() + Duration::from_secs(
Duration::from_secs(ttl.as_secs().checked_shr(exp).unwrap_or(0))

ttl.as_secs()
.checked_shr(factor)
.unwrap_or(0),
))
}

impl<TStore> NetworkBehaviour for Kademlia<TStore>
where
for<'a> TStore: RecordStore<'a>,
Expand Down Expand Up @@ -1911,4 +1916,3 @@ impl QueryInfo {
}
}
}

12 changes: 12 additions & 0 deletions protocols/kad/src/behaviour/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,3 +682,15 @@ fn exceed_jobs_max_queries() {
})
)
}

#[test]
mxinden marked this conversation as resolved.
Show resolved Hide resolved
fn exp_decr_expiration_overflow() {
fn prop_no_panic(ttl: Option<Duration>, factor: u32) {
exp_decr_expiration(ttl, factor);
}

// Right shifting a u64 by >63 results in a panic.
prop_no_panic(KademliaConfig::default().record_ttl, 64);

quickcheck(prop_no_panic as fn(_, _))
}