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

fix: time-impurity within boringtun library #45

Merged
merged 25 commits into from
Jan 7, 2025

Conversation

thomaseizinger
Copy link
Member

@thomaseizinger thomaseizinger commented Jan 7, 2025

In its current implementation, boringtun is impure w.r.t. time because it calls Instant::now and Instant::elapsed internally. To test time-related functionality, the mock-instant dependency is currently used.

This is unnecessary and can also be solved by introducing Instant parameters to all necessary functions.

This PR does that in a fully-backwards compatible way by introducing _at functions for all necessary public functions that require the current time.

Resolves: #41.

@thomaseizinger thomaseizinger changed the title refactor: fix time-impurity fix: time-impurity within boringtun library Jan 7, 2025
@thomaseizinger thomaseizinger force-pushed the feat/fix-time-impurity branch from f4bc849 to 4987ed2 Compare January 7, 2025 12:10
@thomaseizinger thomaseizinger added this pull request to the merge queue Jan 7, 2025
Merged via the queue into master with commit fe67002 Jan 7, 2025
13 checks passed
github-merge-queue bot pushed a commit that referenced this pull request Jan 7, 2025
As per the WireGuard spec
(https://www.wireguard.com/papers/wireguard.pdf#page=14), every time an
expiring timer causes a handshake initiation, a random jitter between 0
and 333ms should be added. This prevents nodes from attempting to
re-initiate a new session at the same time. In Firezone's
strongly-deterministic test suite, this is triggered very easily because
we advance time at the exact same pace.

In order to make use of the deterministic functions added in #45 within
Firezone, this needs to be fixed.

Generating the jitter requires randomness. In order to be deterministic,
a new `rng_seed` parameter is added to the `Tunn::new_at` function. An
observer cannot guess this seed and thus, will be unable to proactively
attempt to initiate handshakes at the same time as the current node.

Fixes: #19.
github-merge-queue bot pushed a commit that referenced this pull request Jan 9, 2025
With #45, the need for mocking `Instant` in order to test time-related
functionality has been obsoleted. All functions simply take `Instant` as
a parameter now. In order to preserve backwards-compatibility, an empty
feature has been added that replaces the optional dependency.
github-merge-queue bot pushed a commit that referenced this pull request Jan 13, 2025
At present, `boringtun`'s time handling works by storing the "current"
time on every update to `update_timers_at`. This time is then used to
timestamp sessions, keep-alives etc.

This isn't as precise as we can be! With #45, we now track time as an
explicit dependency to all functions, meaning functions such as handling
an incoming packet already have access to precisely the current time! We
can leverage this to use that provided timestamp instead of keeping
around the `TimeCurrent` timer.

This results in a few more parameters here and there but makes reasoning
about time within `boringtun` easier.

Depends-On: #58.
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.

merge feat/fix-time-impurity
1 participant