aarch64-dit crate is missing fencing (for both compiler and CPU)
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Accessibilité débutants
- 35/100
- Type d'issue
- Bug
- Clarté
- Plutôt claire
- Activité
- Active
- Stack technique
- rust
- Domaine
- cryptography, security
Piste de recherche
Commencez par examiner le crate aarch64-dit et les exemples Godbolt liés afin de vérifier que la réorganisation du compilateur peut déplacer des opérations sensibles en dehors de la région DIT. Examinez les recommandations Apple liées ainsi que le patch de comparaison proposé concernant la barrière contre la spéculation et la prise en charge de cpufeatures. Le travail est considéré comme terminé lorsque la spéculation du compilateur et celle du CPU sont correctement bloquées, et que l’API d’activation ainsi que ses implications en matière de sécurité sont résolues.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
The aarch64-dit crate has two notable flaws:
- It is possible for the compiler to reorder operations outside the critical section where DIT is enabled.
- It does not perform speculation fencing, which is documented as necessary at least on apple platforms in this document: https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations.
Flaw 1: Compiler Reordering
Here's an example of the first flaw: https://godbolt.org/z/8fdsnxrc5. Essentially the compiler is free to move instructions outside of the asm!("msr DIT #1") and asm!("msr DIT #0") guarded region, and you can see that it essentially compiles:
if have_dit {
dit_enable();
}
let res = sensitive(a, b);
if have_dit {
dit_disable();
}
return res;
into
if have_dit {
dit_enable();
dit_disable();
}
return sensitive(a, b);
which defeats the point, since the instructions which are data-dependent are no longer in the block where DIT is enabled.
In an ideal world, inserting compiler fences (or atomic fences, or adjusting the asm! options...) would fix this, however they only apply to memory accesses, so that does not work. The only approach I can think of that does work is to use an indirect jump whose target we obscure from the vision of the compiler. Something like:
impl Dit {
// ...snip ...
/// Call `f` with DIT enabled, returning the result. Takes care to ensure
/// that that any computations which occur during `f` do not leak outside
/// the critical section.
pub fn with<R, F: FnOnce() -> R>(s: &self, f: F) -> R {
let guard = self.enable();
let mut o: Option<F> = Some(f);
// Note: Could be unwrap_unchecked().
let f: &mut dyn FnMut() -> R = &mut || o.take().unwrap()();
core::hint::black_box(f)()
}
// ...snip...
}
This works, as demonstrated here: https://godbolt.org/z/fv1Gofjxc (note: the add instruction was changed to floating point addition just so that it's clearer in the assembly listing what is happening—you can see that the fadd takes place in the indirectly called function, which is called (the blr x8) between the msr DIT #1; and msr DIT #0 instructions.
Flaw 2: Speculation
I'm unsure if this is needed on targets besides apple (my suspicion is yes, or at least that there's no guarantee for it to be a no), but apple documents it as necessary in the linked documentation.
Essentially, you need something like this after you enable DIT, and before you disable it (actually, I think you might not need it before disabling it, since it's fine if a DIT-mode processor speculates operations outside of DIT mode).
if is_aarch64_feature_detected!("sb") {
// note: in the real impl this must happen
// in a `#[target_feature(enable = "sb")]` function)
asm!("sb");
} else {
asm!("dsb nsh", "isb sy");
}
Of course, a real impl needs to:
- Put
asm!("sb")inside a#[target_feature(enable = "sb")]function. - Use
cpufeaturesrather thanis_aarch64_feature_detected(which requires addingsbsupport tocpufeatures). - And so on.
A patch fixing these issues is here: https://github.com/RustCrypto/utils/compare/master...thomcc:rust-crypto-utils:aarch64-dit-sb-fix. I don't have time to do a PR and respond to review feedback, feel free to mess with it however you please, or to take a different approach that solves the same problem.
One issue with my patch is that it leaves the Dit::enable() function present and non-deprecated, even though it's a footgun. I think probably the right call would be to remove it. But that begs the question of whether or not the API should have a different structure in other ways too.
- Langage dominant
- Rust
- Étoiles
- 674
- Forks
- 170
- Merge moyen
- 1 j 12 h
- PR mergées (30 j)
- 10
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de RustCrypto/utils
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
RustCrypto/utils#1546 · 1 commentaire ·
-
Difficulté 3/5 1-2 jours Accessibilité débutants 55/100
RustCrypto/utils#1537 · 7 commentaires ·
-
Difficulté 4/5 3-5 jours Accessibilité débutants 45/100
RustCrypto/utils#1534 · 2 commentaires ·
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 45/100
RustCrypto/utils#1529 · 4 commentaires ·
-
x86: guard CPUID leaf 7 by the maximum basic leaf; AVX detection may need XMM+YMM XCR0 state Ouverte
Difficulté 4/5 3-5 jours Accessibilité débutants 45/100
RustCrypto/utils#1510 · 1 commentaire ·
Toutes les issues de RustCrypto/utils
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
-
bug core
Difficulté 2/5 1-3 heures Accessibilité débutants 86/100
-
JIT-compiled number -> Decimal conversion silently overflows instead of raising DECIMAL_OVERFLOW Ouvertefuzz
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
ClickHouse/ClickHouse#122114 ·
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 92/100
linebender/vello_svg#90 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100