diff --git a/Makefile b/Makefile index 51cc6de..c78726b 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CXX = clang++ CC = clang -SOURCES := targets/psbt.cpp targets/bech32.cpp targets/tx_des.cpp targets/miniscript_string.cpp targets/block_des.cpp targets/prefilledtransaction.cpp +SOURCES := targets/script.cpp targets/psbt.cpp targets/bech32.cpp targets/tx_des.cpp targets/miniscript_string.cpp targets/block_des.cpp targets/prefilledtransaction.cpp INCLUDES = dependencies/ dependencies/bitcoin/src/ dependencies/bitcoin/src/secp256k1/include LIB_DIR = dependencies/bitcoin/src/ dependencies/bitcoin/src/.libs dependencies/bitcoin/src/secp256k1/.libs rust_bitcoin_lib/target/debug btcd_lib OBJS := $(patsubst %.cpp, build/%.o, $(SOURCES)) diff --git a/fuzzer.cpp b/fuzzer.cpp index aa3725f..f72f88b 100644 --- a/fuzzer.cpp +++ b/fuzzer.cpp @@ -12,6 +12,7 @@ #include "targets/tx_des.h" #include "targets/bech32.h" #include "targets/psbt.h" +#include "targets/script.h" extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FuzzedDataProvider provider(data, size); @@ -31,6 +32,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { Bech32(provider); } else if (target == "psbt") { Psbt(provider); + } else if (target == "script") { + Script(provider); } return 0; // Values other than 0 and -1 are reserved for future use. diff --git a/rust_bitcoin_lib/rust_bitcoin_lib.h b/rust_bitcoin_lib/rust_bitcoin_lib.h index 8e0f382..a786782 100644 --- a/rust_bitcoin_lib/rust_bitcoin_lib.h +++ b/rust_bitcoin_lib/rust_bitcoin_lib.h @@ -1,6 +1,7 @@ #include extern bool rust_miniscript_from_str(const char* miniscript_str); +extern bool rust_bitcoin_script(const char* miniscript_str); extern char* rust_bitcoin_psbt(const char* miniscript_str); extern char* rust_miniscript_from_str_check_key(const char* miniscript_str); extern char* rust_bitcoin_des_block(const uint8_t *data, size_t len); diff --git a/rust_bitcoin_lib/src/lib.rs b/rust_bitcoin_lib/src/lib.rs index fa04b41..85d4247 100644 --- a/rust_bitcoin_lib/src/lib.rs +++ b/rust_bitcoin_lib/src/lib.rs @@ -7,6 +7,7 @@ use std::str::Utf8Error; use bitcoin::bip152::PrefilledTransaction; use bitcoin::consensus::deserialize_partial; +use bitcoin::consensus::encode; use bitcoin::Block; use miniscript::bitcoin::script; use miniscript::bitcoin::secp256k1::XOnlyPublicKey; @@ -100,6 +101,23 @@ pub unsafe extern "C" fn rust_bitcoin_psbt(data: *const u8, len: usize) -> *mut } } +#[no_mangle] +pub unsafe extern "C" fn rust_bitcoin_script(data: *const u8, len: usize) -> bool { + // Safety: Ensure that the data pointer is valid for the given length + let data_slice = slice::from_raw_parts(data, len); + + let script: Result<(bitcoin::script::ScriptBuf, usize), encode::Error> = encode::deserialize_partial(data_slice); + match script { + Err(_) => false, + Ok(s) => { + if s.0.is_op_return() || s.0.len() > 10_000 { + return false + } + true + } + } +} + #[no_mangle] pub unsafe extern "C" fn rust_miniscript_from_str_check_key(input: *const c_char) -> *mut c_char { let Ok(desc) = c_str_to_str(input) else { diff --git a/targets/script.cpp b/targets/script.cpp new file mode 100644 index 0000000..44cfd67 --- /dev/null +++ b/targets/script.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +#include "script.h" +#include "bitcoin/src/streams.h" +#include "bitcoin/src/script/script.h" + +extern "C" bool rust_bitcoin_script(uint8_t *data, size_t len); + +bool CoreScript(Span buffer) +{ + DataStream ds{buffer}; + CScript script; + try { + ds >> script; + } catch (const std::ios_base::failure& e) { + return false; + } + if (script.IsUnspendable()) return false; + return true; +} + + +void Script(FuzzedDataProvider& provider) +{ + std::vector buffer{provider.ConsumeRemainingBytes()}; + bool core{CoreScript(buffer)}; + bool rust_bitcoin{rust_bitcoin_script(buffer.data(), buffer.size())}; + assert(core == rust_bitcoin); +} \ No newline at end of file diff --git a/targets/script.h b/targets/script.h new file mode 100644 index 0000000..fcf8efb --- /dev/null +++ b/targets/script.h @@ -0,0 +1,7 @@ +#ifndef SCRIPT_H +#define SCRIPT_H + +#include + +void Script(FuzzedDataProvider& provider); +#endif \ No newline at end of file