Skip to content

[RTE-513] Bugfix: Ensure sleep takes long enough on SGX platform #144357

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

raoulstrackx
Copy link
Contributor

@raoulstrackx raoulstrackx commented Jul 23, 2025

Summary
The thread::sleep function documentation states:

Puts the current thread to sleep for at least the specified amount of time.

And for thread::sleep_until:

Puts the current thread to sleep until the specified deadline has passed.

These did not hold for the x86_64-fortanix-unknown-sgx platform. This PR fixes this issue.

More details
The attack model on Intel SGX states that enclaves should not rely on timing to be accurate within an enclave. To ensure that users do not rely on the accuracy of the wait usercall (see the ABI), we randomized the specified timeout with +/-10%. This was causing issues when the timeout was reduced. This PR fixes the issue by picking a random duration of up to +10% (i.e., the duration will never be reduced).
The issue was caught when #141829 added a test for sleep_until. There wasn't a test in the standard library to verify that thread::sleep sleeps for at least the specified amount of time. Such a test is added as well.

cc: @jethrogb @aditijannu

@rustbot
Copy link
Collaborator

rustbot commented Jul 23, 2025

r? @ibraheemdev

rustbot has assigned @ibraheemdev.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added O-SGX Target: SGX S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-SGX Target: SGX S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants