Skip to content

Commit 85075a6

Browse files
committed
replace fuzztarget Cargo feature with a rustc --cfg flag
It's super dangerous to use Cargo features for this, since they can be set accidentally (or maliciously by any crate in a user's entire dep tree). Instead we can just require users set `RUSTFLAGS` appropriately, which we can easily do in our fuzzing scripts.
1 parent d77483f commit 85075a6

File tree

6 files changed

+20
-15
lines changed

6 files changed

+20
-15
lines changed

Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ endomorphism = ["secp256k1-sys/endomorphism"]
2626
lowmemory = ["secp256k1-sys/lowmemory"]
2727
global-context = ["std", "rand-std"]
2828

29-
# Do not use this feature! HAZMAT. (meant for Fuzzing only. this is *BROKEN CRYPTOGRAPHY*)
30-
fuzztarget = ["secp256k1-sys/fuzztarget"]
31-
3229
[dependencies]
3330
secp256k1-sys = { version = "0.3.1", default-features = false, path = "./secp256k1-sys" }
3431
bitcoin_hashes = { version = "0.9", optional = true }

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,12 @@ before_script:
3535
cargo generate-lockfile --verbose && cargo update -p cc --precise "1.0.41" --verbose;
3636
fi
3737
```
38+
39+
## Fuzzing
40+
41+
If you want to fuzz this library, or any library which depends on it, you will
42+
probably want to disable the actual cryptography, since fuzzers are unable to
43+
forge signatures and therefore won't test many interesting codepaths. To instead
44+
use a trivially-broken but fuzzer-accessible signature scheme, compile with
45+
`--cfg=rust_secp_fuzz` in your `RUSTFLAGS` variable.
46+

contrib/test.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ if [ "$DO_FEATURE_MATRIX" = true ]; then
3131
done
3232

3333
# Other combos
34-
cargo test --no-run --verbose --features="fuzztarget"
35-
cargo test --no-run --verbose --features="fuzztarget recovery"
34+
RUSTFLAGS='--cfg=rust_secp_fuzz' cargo test --no-run --verbose
35+
RUSTFLAGS='--cfg=rust_secp_fuzz' cargo test --no-run --verbose --features="recovery"
3636
cargo test --verbose --features="rand rand-std"
3737
cargo test --verbose --features="rand serde"
3838

secp256k1-sys/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,3 @@ endomorphism = []
3131
lowmemory = []
3232
std = []
3333

34-
# Do not use this feature! HAZMAT. (meant for Fuzzing only. this is *BROKEN CRYPTOGRAPHY*)
35-
fuzztarget = []

secp256k1-sys/src/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub type SchnorrNonceFn = unsafe extern "C" fn(
9393
#[derive(Clone, Debug)]
9494
#[repr(C)] pub struct Context(c_int);
9595

96-
#[cfg(feature = "fuzztarget")]
96+
#[cfg(rust_secp_fuzz)]
9797
impl Context {
9898
pub fn flags(&self) -> u32 {
9999
self.0 as u32
@@ -260,7 +260,7 @@ impl hash::Hash for KeyPair {
260260
}
261261
}
262262

263-
#[cfg(not(feature = "fuzztarget"))]
263+
#[cfg(not(rust_secp_fuzz))]
264264
extern "C" {
265265
/// Default ECDH hash function
266266
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_3_1_ecdh_hash_function_default")]
@@ -674,7 +674,7 @@ impl<T> CPtr for [T] {
674674

675675

676676

677-
#[cfg(feature = "fuzztarget")]
677+
#[cfg(rust_secp_fuzz)]
678678
mod fuzz_dummy {
679679
extern crate std;
680680
use self::std::{ptr, mem};
@@ -1156,7 +1156,8 @@ mod fuzz_dummy {
11561156
unimplemented!();
11571157
}
11581158
}
1159-
#[cfg(feature = "fuzztarget")]
1159+
1160+
#[cfg(rust_secp_fuzz)]
11601161
pub use self::fuzz_dummy::*;
11611162

11621163

secp256k1-sys/src/recovery.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! # FFI of the recovery module
1717
1818
use ::types::*;
19-
#[cfg(not(feature = "fuzztarget"))]
19+
#[cfg(not(rust_secp_fuzz))]
2020
use ::{Context, Signature, NonceFn, PublicKey};
2121

2222
/// Library-internal representation of a Secp256k1 signature + recovery ID
@@ -36,7 +36,7 @@ impl Default for RecoverableSignature {
3636
}
3737
}
3838

39-
#[cfg(not(feature = "fuzztarget"))]
39+
#[cfg(not(rust_secp_fuzz))]
4040
extern "C" {
4141
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_3_1_ecdsa_recoverable_signature_parse_compact")]
4242
pub fn secp256k1_ecdsa_recoverable_signature_parse_compact(cx: *const Context, sig: *mut RecoverableSignature,
@@ -70,7 +70,7 @@ extern "C" {
7070
}
7171

7272

73-
#[cfg(feature = "fuzztarget")]
73+
#[cfg(rust_secp_fuzz)]
7474
mod fuzz_dummy {
7575
extern crate std;
7676
use self::std::ptr;
@@ -126,6 +126,6 @@ mod fuzz_dummy {
126126
unimplemented!();
127127
}
128128
}
129-
#[cfg(feature = "fuzztarget")]
129+
#[cfg(rust_secp_fuzz)]
130130
pub use self::fuzz_dummy::*;
131131

0 commit comments

Comments
 (0)