Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Unreleased

- Evaluate satisfaction and dissatisfaction once per Miniscript node in post-order,
avoiding exponential recomputation in nested branches. Child outcomes are released
as soon as their parent consumes them, keeping peak memory proportional to the live
node frontier; evaluation no longer recurses through subtrees.
- Enforce the context resource limits that hold for every satisfaction path, such as
the opcode-count limits, before the satisfier is queried, including for scripts
decoded with relaxed parsing options. Limits that depend on the chosen path, such
as the Segwit witness-item, Legacy scriptSig and Taproot stack-size limits, are
enforced against the satisfaction that was actually built, so scripts with an
over-limit branch can still be satisfied through an in-limit path. Resource-invalid
satisfaction returns a context error; planning templates are impossible. This also
protects descriptor planning and PSBT finalization.

# # 12.3.1 - April 1, 2025

- Silent fix for CVE-2025-43707 (crash on satisfaction of particularly crafted `thresh` fragments) [#798](https://github.com/rust-bitcoin/rust-miniscript/pull/798)
Expand Down
102 changes: 100 additions & 2 deletions src/miniscript/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use crate::miniscript::limits::{
MAX_OPS_PER_SCRIPT, MAX_SCRIPTSIG_SIZE, MAX_SCRIPT_ELEMENT_SIZE, MAX_SCRIPT_SIZE,
MAX_STACK_SIZE, MAX_STANDARD_P2WSH_SCRIPT_SIZE, MAX_STANDARD_P2WSH_STACK_ITEMS,
};
use crate::miniscript::types;
use crate::miniscript::{satisfy, types};
use crate::prelude::*;
use crate::util::witness_to_scriptsig;
use crate::util::{witness_to_scriptsig, ItemSize};
use crate::{hash256, Error, ForEachKey, Miniscript, MiniscriptKey, Terminal};

/// Error for Script Context
Expand Down Expand Up @@ -258,6 +258,40 @@ where
Ok(())
}

/// Check the context resource limits that hold for the script itself,
/// regardless of which satisfaction path is chosen, such as the
/// opcode-count limits. Since no satisfaction path can avoid these
/// limits, they may be enforced before the satisfier queries the
/// provider.
///
/// Defaults to the global checks plus the local consensus checks, which
/// are path-independent for the pre-Segwit-v1 contexts. Contexts whose
/// local consensus limits depend on the chosen satisfaction path, like
/// the Taproot stack-size limits, must override this method to exclude
/// those limits, which are then enforced on the chosen satisfaction by
/// [`ScriptContext::check_satisfaction_validity`].
fn check_static_validity<Pk: MiniscriptKey>(
ms: &Miniscript<Pk, Self>,
) -> Result<(), ScriptContextError> {
Self::check_global_consensus_validity(ms)?;
Self::check_global_policy_validity(ms)?;
Self::check_local_consensus_validity(ms)
}

/// Check the context resource limits that depend on which satisfaction
/// path was chosen, against the satisfaction that was actually built.
/// This allows satisfying scripts that have over-limit satisfaction
/// paths as long as the chosen path itself fits the limits.
///
/// Returns `Ok` for impossible or unavailable satisfactions, which do
/// not produce a witness to check. Defaults to `Ok` for contexts
/// without path-dependent limits.
fn check_satisfaction_validity<Pk: MiniscriptKey>(
_satisfaction: &satisfy::Satisfaction<satisfy::Placeholder<Pk>>,
) -> Result<(), ScriptContextError> {
Ok(())
}

/// Check the consensus + policy(if not disabled) rules that are not based
/// satisfaction
fn check_global_validity<Pk: MiniscriptKey>(
Expand Down Expand Up @@ -471,6 +505,23 @@ impl ScriptContext for Legacy {
ms.ext.max_sat_size.map(|x| x.1)
}

fn check_satisfaction_validity<Pk: MiniscriptKey>(
satisfaction: &satisfy::Satisfaction<satisfy::Placeholder<Pk>>,
) -> Result<(), ScriptContextError> {
if let satisfy::Witness::Stack(stack) = &satisfaction.stack {
// The typechecker accounts for the scriptSig cost as the sum of
// the push-encoded witness items of the satisfaction path.
let script_sig_size = stack.iter().map(|item| item.size()).sum::<usize>();
if script_sig_size > MAX_SCRIPTSIG_SIZE {
return Err(ScriptContextError::MaxScriptSigSizeExceeded {
actual: script_sig_size,
limit: MAX_SCRIPTSIG_SIZE,
});
}
}
Ok(())
}

fn pk_len<Pk: MiniscriptKey>(pk: &Pk) -> usize {
if pk.is_uncompressed() {
66
Expand Down Expand Up @@ -599,6 +650,23 @@ impl ScriptContext for Segwitv0 {
ms.ext.max_sat_size.map(|x| x.0)
}

fn check_satisfaction_validity<Pk: MiniscriptKey>(
satisfaction: &satisfy::Satisfaction<satisfy::Placeholder<Pk>>,
) -> Result<(), ScriptContextError> {
if let satisfy::Witness::Stack(stack) = &satisfaction.stack {
// Account for the witness script item in the same way as
// `max_satisfaction_witness_elements`.
let witness_items = stack.len() + 1;
if witness_items > MAX_STANDARD_P2WSH_STACK_ITEMS {
return Err(ScriptContextError::MaxWitnessItemsExceeded {
actual: witness_items,
limit: MAX_STANDARD_P2WSH_STACK_ITEMS,
});
}
}
Ok(())
}

fn pk_len<Pk: MiniscriptKey>(_pk: &Pk) -> usize { 34 }

fn name_str() -> &'static str { "Segwitv0" }
Expand Down Expand Up @@ -713,6 +781,36 @@ impl ScriptContext for Tap {
Ok(())
}

fn check_static_validity<Pk: MiniscriptKey>(
ms: &Miniscript<Pk, Self>,
) -> Result<(), ScriptContextError> {
// The Taproot stack-size limits depend on the chosen satisfaction
// path, so they are enforced against the built satisfaction by
// `check_satisfaction_validity` instead of up front.
Self::check_global_consensus_validity(ms)?;
Self::check_global_policy_validity(ms)
}

fn check_satisfaction_validity<Pk: MiniscriptKey>(
satisfaction: &satisfy::Satisfaction<satisfy::Placeholder<Pk>>,
) -> Result<(), ScriptContextError> {
if let satisfy::Witness::Stack(stack) = &satisfaction.stack {
// The witness elements all reach the stack before the script
// executes, so a satisfaction with more elements than the
// stack limit is consensus-invalid whichever path is chosen.
// The stack usage during execution of the chosen path is
// enforced against the same limit when the witness is
// validated.
if stack.len() > MAX_STACK_SIZE {
return Err(ScriptContextError::StackSizeLimitExceeded {
actual: stack.len(),
limit: MAX_STACK_SIZE,
});
}
}
Ok(())
}

fn max_satisfaction_size<Pk: MiniscriptKey>(ms: &Miniscript<Pk, Self>) -> Option<usize> {
// The witness stack cost is the first element of the tuple
ms.ext.max_sat_size.map(|x| x.0)
Expand Down
Loading
Loading