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

Add BlackBox #123

Merged
merged 1 commit into from
Jun 19, 2024
Merged

Conversation

tarcieri
Copy link
Contributor

Adds a VolatileCell-like struct which introduces an optimization barrier on all accesses.

The *Cell-like get() method is the only accessor for the inner value and uses a more generalized (i.e. for all Copy types) black_box to preclude optimizations.

This is useful for things like bitwise mask values to ensure LLVM doesn't try to optimize around special cases like 0, especially in the context of loops.

For an example use case, see:

https://rustsec.org/advisories/RUSTSEC-2024-0344.html

@tarcieri tarcieri changed the base branch from main to develop June 19, 2024 02:49
// Unsafe is ok, because:
// - &input is not NULL;
// - size of input is not zero;
// - u8 is neither Sync, nor Send;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

See also #91

src/lib.rs Outdated Show resolved Hide resolved
@tarcieri
Copy link
Contributor Author

tarcieri commented Jun 19, 2024

Note: this PR could use a constructor and a test

Edit: added in c0774e9

@tarcieri tarcieri force-pushed the black-box branch 3 times, most recently from 45f03d4 to 068de49 Compare June 19, 2024 21:50
Adds a `VolatileCell`-like struct which introduces an optimization
barrier on all accesses.

The `*Cell`-like `get()` method is the only accessor for the inner value
and uses a more generalized (i.e. for all `Copy` types) `black_box`
to preclude optimizations.

This is useful for things like bitwise mask values to ensure LLVM
doesn't try to optimize around special cases like `0`, especially in the
context of loops.

For an example use case, see:

https://rustsec.org/advisories/RUSTSEC-2024-0344.html
@isislovecruft isislovecruft merged commit 3e8d92b into dalek-cryptography:develop Jun 19, 2024
3 of 4 checks passed
@tarcieri tarcieri deleted the black-box branch June 19, 2024 22:30
tarcieri added a commit to dalek-cryptography/curve25519-dalek that referenced this pull request Jun 20, 2024
Replaces the security mitigation added in #659 and #661 for
masking-related timing variability which used an inline `black_box`
using the recently added `subtle::BlackBox` newtype (see
dalek-cryptography/subtle#123)

Internally `BlackBox` uses a volatile read by default (i.e. same
strategy which was used before) or when the `core_hint_black_box`
feature of `subtle` is enabled, it uses `core::hint::black_box`
(whose documentation was recently updated to reflect the nuances of
potential cryptographic use, see rust-lang/rust#126703)

This PR goes ahead and uses `BlackBox` for both `mask` and
`underflow_mask` where previously it was only used on `underflow_mask`.
The general pattern of bitwise masking inside a loop seems worrisome for
the optimizer potentially inserting branches in the future.

Below are godbolt inspections of the generated assembly, which are free
of the `jns` instructions originally spotted in #659/#661:

- 32-bit (read_volatile): https://godbolt.org/z/TKo9fqza4
- 32-bit (hint::black_box): https://godbolt.org/z/caoMxYbET
- 64-bit (read_volatile): https://godbolt.org/z/PM6zKjj1f
- 64-bit (hint::black_box): https://godbolt.org/z/nseaPvdWv
tarcieri added a commit to tarcieri/subtle that referenced this pull request Jun 21, 2024
Seems some changes I force pushed to dalek-cryptography#123 didn't wind up getting merged.
In that PR, I noted that `pub const fn new` was MSRV breaking and got
rid of the `const` but that didn't end up in `develop`.

I also encountered a build failure on `develop` since the legacy
`black_box` function wasn't gated on the `core_hint_black_box` feature
and thus clashed with `core::hint::black_box` when it was imported:

```
   Compiling subtle v2.6.0 (/Users/tarcieri/src/subtle)
error[E0255]: the name `black_box` is defined multiple times
   --> src/lib.rs:223:1
    |
100 | use core::hint::black_box;
    |     --------------------- previous import of the value `black_box` here
...
223 | fn black_box<T: Copy>(input: T) -> T {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `black_box` redefined here
    |
    = note: `black_box` must be defined only once in the value namespace of this module
help: you can use `as` to change the binding name of the import
    |
100 | use core::hint::black_box as other_black_box;
    |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

warning: unused import: `core::hint::black_box`
   --> src/lib.rs:100:5
    |
100 | use core::hint::black_box;
    |     ^^^^^^^^^^^^^^^^^^^^^
    |
    = note: `#[warn(unused_imports)]` on by default
```

This is a breaking change since we shipped a `const fn` for
`BlackBox::new` already, so I'd suggest releasing this as 2.6.1 and
yanking 2.6.0 for being unintentionally MSRV breaking.
tarcieri added a commit to dalek-cryptography/curve25519-dalek that referenced this pull request Jun 24, 2024
Replaces the security mitigation added in #659 and #661 for
masking-related timing variability which used an inline `black_box`
using the recently added `subtle::BlackBox` newtype (see
dalek-cryptography/subtle#123)

Internally `BlackBox` uses a volatile read by default (i.e. same
strategy which was used before) or when the `core_hint_black_box`
feature of `subtle` is enabled, it uses `core::hint::black_box`
(whose documentation was recently updated to reflect the nuances of
potential cryptographic use, see rust-lang/rust#126703)

This PR goes ahead and uses `BlackBox` for both `mask` and
`underflow_mask` where previously it was only used on `underflow_mask`.
The general pattern of bitwise masking inside a loop seems worrisome for
the optimizer potentially inserting branches in the future.

Below are godbolt inspections of the generated assembly, which are free
of the `jns` instructions originally spotted in #659/#661:

- 32-bit (read_volatile): https://godbolt.org/z/TKo9fqza4
- 32-bit (hint::black_box): https://godbolt.org/z/caoMxYbET
- 64-bit (read_volatile): https://godbolt.org/z/PM6zKjj1f
- 64-bit (hint::black_box): https://godbolt.org/z/nseaPvdWv
rozbb pushed a commit to dalek-cryptography/curve25519-dalek that referenced this pull request Jun 24, 2024
Replaces the security mitigation added in #659 and #661 for
masking-related timing variability which used an inline `black_box`
using the recently added `subtle::BlackBox` newtype (see
dalek-cryptography/subtle#123)

Internally `BlackBox` uses a volatile read by default (i.e. same
strategy which was used before) or when the `core_hint_black_box`
feature of `subtle` is enabled, it uses `core::hint::black_box`
(whose documentation was recently updated to reflect the nuances of
potential cryptographic use, see rust-lang/rust#126703)

This PR goes ahead and uses `BlackBox` for both `mask` and
`underflow_mask` where previously it was only used on `underflow_mask`.
The general pattern of bitwise masking inside a loop seems worrisome for
the optimizer potentially inserting branches in the future.

Below are godbolt inspections of the generated assembly, which are free
of the `jns` instructions originally spotted in #659/#661:

- 32-bit (read_volatile): https://godbolt.org/z/TKo9fqza4
- 32-bit (hint::black_box): https://godbolt.org/z/caoMxYbET
- 64-bit (read_volatile): https://godbolt.org/z/PM6zKjj1f
- 64-bit (hint::black_box): https://godbolt.org/z/nseaPvdWv
jmwample pushed a commit to jmwample/curve25519-dalek that referenced this pull request Jun 26, 2024
…y#662)

Replaces the security mitigation added in dalek-cryptography#659 and dalek-cryptography#661 for
masking-related timing variability which used an inline `black_box`
using the recently added `subtle::BlackBox` newtype (see
dalek-cryptography/subtle#123)

Internally `BlackBox` uses a volatile read by default (i.e. same
strategy which was used before) or when the `core_hint_black_box`
feature of `subtle` is enabled, it uses `core::hint::black_box`
(whose documentation was recently updated to reflect the nuances of
potential cryptographic use, see rust-lang/rust#126703)

This PR goes ahead and uses `BlackBox` for both `mask` and
`underflow_mask` where previously it was only used on `underflow_mask`.
The general pattern of bitwise masking inside a loop seems worrisome for
the optimizer potentially inserting branches in the future.

Below are godbolt inspections of the generated assembly, which are free
of the `jns` instructions originally spotted in dalek-cryptography#659/dalek-cryptography#661:

- 32-bit (read_volatile): https://godbolt.org/z/TKo9fqza4
- 32-bit (hint::black_box): https://godbolt.org/z/caoMxYbET
- 64-bit (read_volatile): https://godbolt.org/z/PM6zKjj1f
- 64-bit (hint::black_box): https://godbolt.org/z/nseaPvdWv
redshiftzero added a commit to redshiftzero/subtle-ng that referenced this pull request Aug 14, 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